home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevupd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  240.9 KB  |  7,173 lines

  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /* $Id: gdevupd.c,v 1.3 2000/09/19 19:00:23 lpd Exp $ */
  20. /* "uniprint" -- Ugly Printer Driver by Gunther Hess (gunther@elmos.de) */
  21.  
  22. /* Revision-History:
  23.    23-Mar-1997 -  1.43: First published version
  24.    24-Mar-1997 -  1.44: gs4.03 compatible version on the web
  25.    31-Mar-1997 -  1.53: First Version inside gs-fileset (limited)
  26.    28-Apr-1997 -  1.54: Version intended for public gs-release
  27.     4-May-1997 -  1.55: Deactivated an accidentially active Debug-Option
  28.    14-Jun-1997 -  1.56: Bug-Workaround for White on White Printing (gs5.0)
  29.    17-Jun-1997 -  1.57: More reasonable Fix for the above Bug
  30.    ...
  31.     7-Jul-1997 -  1.68: NULL-Param-BUG, HR's BJC, Pwidth/-height BUG, YFlip
  32.    25-Jul-1997 -  1.69: Bug-Fix: incomplete Change of PHEIGHT-Treatment
  33.     4-Aug-1997 -  1.70: Arrgh: still incomplete Change of PHEIGHT-Treatment
  34.    17-AUG-1997 -  1.71: Fix of BSD-sprintf bug. (returns char * there)
  35.    ...
  36.    28-Sep-1997 -  1.77: Fixed the byte<>char and casted-lvalue Problems
  37.    ...
  38.    12-Mar-1998 -  1.80: Some PJL-Functions, Map-Bug-Fix (by Wonder-Wolfgang)
  39.    21-Oct-1998 -  1.81: Added RGB2CMY[_]K Modi (Eric Domenjoud)
  40.    ...
  41.    27-Feb-2000 -  1.84: CMYKgenerate with forced K-Control [distributed]
  42.  
  43. */
  44.  
  45. /* Canon BJC 610 additions from (hr)
  46.       Helmut Riegler <helmut-riegler@net4you.co.at>
  47.  
  48.    The BJC-4000 can be supported very easily, only by creating the right .upp
  49.    parameter file. If you have this printer and you are willing to do this,
  50.    contact me, I'll give you the technical details (ESC codes).
  51. */
  52.  
  53. /* ------------------------------------------------------------------- */
  54. /* Compile-Time-Options                                                */
  55. /* ------------------------------------------------------------------- */
  56.  
  57. /**
  58. There are two compile-time options for this driver:
  59.    1. UPD_SIGNAL   enables interrupt detection, that aborts printing and
  60.    2. UPD_MESSAGES controls the amount of messages generated by the driver
  61. */
  62.  
  63. #ifndef   UPD_SIGNAL
  64. #ifdef      __unix__
  65. #define       UPD_SIGNAL 1 /** Activated, if undefined, on UNIX-Systems */
  66. #else  /*  !__unix__ */
  67. #define       UPD_SIGNAL 0 /** Inactive on others, by default */
  68. #endif /*  ?__unix__ */
  69. #endif /* UPD_SIGNAL */
  70.  
  71. #ifndef   UPD_MESSAGES
  72. #define   UPD_MESSAGES UPD_M_ERROR /** Error-messages only, if not defined */
  73. #endif /* UPD_MESSAGES */
  74.  
  75. /* ------------------------------------------------------------------- */
  76. /* Required Header-Files                                               */
  77. /* ------------------------------------------------------------------- */
  78.  
  79. #ifndef   hess_test_INCLUDED /* A private test-Option */
  80.  
  81. #include "gdevprn.h" /** Printer-superclass header */
  82. #include "gsparam.h" /** For the Parameter-Handling (optional) */
  83.  
  84. #include <stdlib.h>  /** for rand */
  85. #include <limits.h>  /** for INT_MIN */
  86. #include <ctype.h>   /** for isupper */
  87.  
  88. #endif /* hess_test_INCLUDED    A private test-Option */
  89.  
  90. #if       UPD_SIGNAL
  91. #include <signal.h> /** Only included, if UPD_SIGNAL is active (true) */
  92. #endif /* UPD_SIGNAL */
  93.  
  94. /* ------------------------------------------------------------------- */
  95. /* Device-Structure (including an additional Structure-Pointer-Type)   */
  96. /* ------------------------------------------------------------------- */
  97.  
  98. typedef struct upd_s upd_t,*upd_p; /** Type & Pointer of device-specifics */
  99. typedef const upd_t *upd_pc;       /** Pointer to constant device-specfics */
  100.  
  101. typedef struct upd_device_s {      /** The driver must typedef ... */
  102.    gx_device_common;               /**    common fields for all devices */
  103.    gx_prn_device_common;           /**    common fields for printing-devices */
  104.    gs_param_string upd_version;    /**    Source-Code Version */
  105.    upd_p           upd;            /**    uniprint-specific extension */
  106. } upd_device;                      /** some type usually  <name>_device> */
  107.  
  108. /* ------------------------------------------------------------------- */
  109. /* Major Driver-Functions                                              */
  110. /* ------------------------------------------------------------------- */
  111.  
  112. private dev_proc_print_page(upd_print_page); /** print a page (required) */
  113.  
  114. private dev_proc_open_device(upd_open);      /** device-initialization (opt) */
  115. private dev_proc_close_device(upd_close);    /** device-release (opt) */
  116.  
  117. private dev_proc_get_params(upd_get_params); /** export parameters (opt) */
  118. private dev_proc_put_params(upd_put_params); /** import parameters (opt) */
  119.  
  120. /**
  121. A `normal' Device-Driver wil only implement one of the following pairs
  122. of functions for the colormapping. But "uniprint" is something special and
  123. it really provides all four reasonable pairs and in addition to that
  124. a fifth set of functions, that delivers better FS-Results with KCMY.
  125.  
  126. The first pair is for the mapping into a single stored component, that
  127. usually represents a grayscale. But nevertheless GHOSTSCRIPT deals with
  128. RGB-Values, but promises to deal with R==G==B-Values when asking to map.
  129.  
  130. The second pair deals with RGB-Values.
  131. */
  132.  
  133. private dev_proc_map_rgb_color( upd_rgb_1color);  /** RGB->Gray-Index */
  134. private dev_proc_map_color_rgb( upd_1color_rgb);  /** Gray-Index->RGB */
  135.  
  136. private dev_proc_map_rgb_color( upd_rgb_3color);  /** RGB->RGB-Index */
  137. private dev_proc_map_color_rgb( upd_3color_rgb);  /** RGB-Index->RGB */
  138.  
  139. /**
  140. The third pair maps RGB-Values into four components, which one might
  141. expect to be KCMY-Values, but they are not: "uniprint" considers this four
  142. Values as White+RGB Values!
  143. */
  144.  
  145. private dev_proc_map_rgb_color( upd_rgb_4color);  /** RGB->WRGB-Index */
  146. private dev_proc_map_color_rgb(upd_4color_rgb);   /** WRGB-Index->RGB */
  147.  
  148. /**
  149. The fourth pair deals with KCMY-Values. The Mapping-Function
  150. is of a different type, due to the additional argument, but the
  151. inverse-Function is of the same type, and expects RGB-Values to be
  152. deliverd into the receiving 3-Component-Array!
  153. */
  154.  
  155. private dev_proc_map_cmyk_color(upd_cmyk_icolor); /** KCMY->KCMY-Index */
  156. private dev_proc_map_color_rgb( upd_icolor_rgb);  /** KCMY->RGB-Index */
  157.  
  158. /**
  159. The difference between the icolor-pair and the kcolor-pair is the enforced
  160. black-generation in the forward-mapping. that is taken into account by the
  161. reverse-mapping too.
  162. */
  163.  
  164. private dev_proc_map_cmyk_color(upd_cmyk_kcolor); /** adds black generation */
  165. private dev_proc_map_color_rgb( upd_kcolor_rgb);  /** watches black-gen */
  166.  
  167. /**
  168. "ovcolor" is CMYK with Black-Generation and Undercolor-Removal, which
  169. is suitable for overprinting:
  170.    CMY' = (CMY-K')/(1-K')
  171. with
  172.    K'   = min(C,M,Y)
  173. */
  174.  
  175. private dev_proc_map_rgb_color(upd_rgb_ovcolor);  /** RGB->CMYK-Index */
  176. #define upd_ovcolor_rgb upd_icolor_rgb            /** CMYK-Index->RGB */
  177.  
  178. /**
  179. "novcolor" is CMYK with Black-Generation and Undercolor-Removal, which
  180. is suitable for CMY / K - Printing:
  181.    CMY' = CMY-K'
  182. with
  183.    K'   = min(C,M,Y)
  184. */
  185.  
  186. private dev_proc_map_rgb_color(upd_rgb_novcolor); /** RGB->CMYK-Index */
  187. #define upd_novcolor_rgb upd_icolor_rgb           /** CMYK-Index->RGB */
  188.  
  189. /**
  190. For the sake of efficiency there is that bunch of functions and they
  191. perform no validity checks, thus it has to be assured that they are
  192. only active, if there is a valid device-structure for then.
  193. upd_procs_map performs this task.
  194. */
  195.  
  196. private int             upd_procs_map( P1(upd_device *udev));
  197.  
  198. /* ------------------------------------------------------------------- */
  199. /* Prototype of the Device-Structure (the only thing exported!)        */
  200. /* ------------------------------------------------------------------- */
  201.  
  202. /**
  203. "uniprint" needs a procedure-table of its own, since it provides several
  204. optional procedures. Simpler-Drivers (e.g. non-color-drivers) may use
  205. prn_std_procs instead of defining their own procedure-table.
  206. */
  207.  
  208. #define upd_set_dev_proc(dev, p, proc) \
  209.    ((dev)->std_procs.p = (dev)->orig_procs.p = (proc))
  210.  
  211. private gx_device_procs upd_procs = {  /** Table of procedures */
  212.    upd_open,                      /** open-function, upd-special */
  213.    gx_default_get_initial_matrix, /** retrieve matrix */
  214.    gx_default_sync_output,        /** sync display */
  215.    gdev_prn_output_page,          /** superclass-print (calls back) */
  216.    upd_close,                     /** close-function, upd-special */
  217.    gx_default_map_rgb_color,      /** RGB-mapping */
  218.    gx_default_map_color_rgb,      /** reverse mapping */
  219.    NULL,                          /** fill_rectangle */
  220.    NULL,                          /** tile_rectangle */
  221.    NULL,                          /** copy_mono */
  222.    NULL,                          /** copy_color */
  223.    NULL,                          /** draw_line */
  224.    gx_default_get_bits,           /** reads scanlines, e.g. for the driver */
  225.    upd_get_params,                /** Export parameters, upd-special */
  226.    upd_put_params,                /** Import parameters, upd-special */
  227.    gx_default_map_cmyk_color      /** KCMY-mapping */
  228. };                                     /** */
  229.  
  230. /**
  231. The prototype-instance of the device-structure _must_ have the name
  232. "gs_uniprint_device", where "uniprint" is the external name of the driver.
  233. This notice is bluntly copied from drivers.txt, which a potential
  234. driver-author should carefully read.
  235.  
  236. Just to mention: this prototype is quite similar to the one, that
  237. "prn_device" produces and it identifies "uniprint" as a monochrome 1Bit
  238. device to GHOSTSCRIPT. But during the lifetime of a driver-instance
  239. this might change.
  240.  
  241. This is the end of the part of declarations, that are common for
  242. color-drivers. The next sections address "uniprint"-specific data-types
  243. and the reader might directly skip to the section titled
  244.  
  245.     upd_print_page: The main workhorse
  246. */
  247.  
  248. upd_device far_data gs_uniprint_device = { /** */
  249.    prn_device_body(upd_device, upd_procs,  /** The Type and Procedures */
  250.       "uniprint",                          /** External name of the Device */
  251.       DEFAULT_WIDTH_10THS,                 /** X-Size (1/10") */
  252.       DEFAULT_HEIGHT_10THS,                /** Y-Size (1/10") */
  253.       72, 72,                              /** X,Y-DpI */
  254.       0.0, 0.0, 0.0, 0.0,                  /** L,B,R,T-Margin */
  255.       1, /**  color_info.num_components 1/3/4 */
  256.       1, /**  color_info.depth         1/2/4/8/16/24/32 */
  257.       1, /**  color_info.max_gray      # of distinct gray levels -1 (255/1) */
  258.       0, /**  color_info.max_color     # of distinct color levels -1 (255/1/0)*/
  259.       1, /**  color_info.dither_grays  size of gray ramp for dithering (5/2) */
  260.       0, /**  color_info.dither_colors size of color cube ditto (5/2/0) */
  261.       upd_print_page),                     /** Print-procedure */
  262.       { NULL, 0, true },                   /** Driver-Version */
  263.       NULL                                 /** upd-field: Initially none */
  264. };                                         /** */
  265.  
  266.  
  267. /* ------------------------------------------------------------------- */
  268. /* UPD-Data- and Prototypes                                            */
  269. /* ------------------------------------------------------------------- */
  270.  
  271. /*@ gdevupd.h < */
  272. /* ------------------------------------------------------------------- */
  273. /* External names of the UPD-Parameters                                */
  274. /* ------------------------------------------------------------------- */
  275.  
  276. /** UPD-Parameters
  277.  
  278. "uniprint" supports a hole bunch of external parameters. This Parameters
  279. fall into the following categories:
  280.  
  281.  0. special-string the upd_version, readonly          upd_version
  282.  1. choice         name-indices, stored in            upd->choice
  283.  2. boolean        single bits, stored in             upd->flags
  284.  3. integers       single numbers, stored in          upd->ints
  285.  4. integer-Arrays arrays of numbers, stored in       upd->int_a
  286.  5. string         device-commands, stored in         upd->strings
  287.  6. string-Arrays  arrayed device-commands, stored in upd->string_a
  288.  7. float-Arrays   arrays of floats, stored in        upd->float_a
  289.  
  290. Currently there is no need for single floats, but they may be introduced in
  291. future versions. Since "uniprint" somtimes manipulates the contents of the
  292. array-variables it dynamically allocates storage for all this parameters.
  293.  
  294. The following sections defines the names for this parameters in the order,
  295. they are stored within the mentioned dynamic fields of the upd-structure.
  296. A NULL-name means that the corresponding parameter is not externally visible.
  297. Besides the name, there is always a symbolic index #defined, that MUST match
  298. the Index-Number of the name.
  299. Actually
  300. */
  301.  
  302. static const char *const upd_version = "upVersion"; /** Readonly Version */
  303.  
  304. /** Names for the multiple-choice-Parameters
  305.  
  306. Currently there are three Parameters, that are handled as named choices.
  307. For each of them, there is an array of constant strings that consists of
  308.  
  309. 1.       the Parameter-Name
  310. 2. - n-1 the available choices.
  311. n.       A terminating NULL
  312. */
  313.  
  314. static const char *const upd_mapper[] = { "upColorModel",
  315. #define MAP_GRAY        1   /** Monochrome & Grayscale Devices */
  316. "DeviceGray",               /** Monochrome & Grayscale Devices */
  317. #define MAP_RGBW        2   /** RGB with White-Generation */
  318. "DeviceRGBW",               /** RGB with White-Generation */
  319. #define MAP_RGB         3   /** RGB-Mapping */
  320. "DeviceRGB",                /** RGB-Mapping */
  321. #define MAP_CMYK        4   /** CMYK-Mapping */
  322. "DeviceCMYK",               /** CMYK-Mapping */
  323. #define MAP_CMYKGEN     5   /** CMYK-Mapping with Black-Generation */
  324. "DeviceCMYKgenerate",       /** CMYK-Mapping with Black-Generation */
  325. #define MAP_RGBOV       6   /** RGB->CMYK with BG and UCR for CMYK */
  326. "DeviceRGB2CMYK",           /** RGB->CMYK with BG and UCR for CMYK */
  327. #define MAP_RGBNOV      7   /** RGB->CMYK with BG and UCR for CMY + K */
  328. "DeviceRGB2CMY_K",          /** RGB->CMYK with BG and UCR for CMY + K */
  329. NULL
  330. };
  331.  
  332. static const char *const upd_render[] = { "upRendering",
  333. #define RND_FSCOMP      1   /** Componentwise Floyd-Steinberg */
  334. "ErrorDiffusion",           /** Componentwise Floyd-Steinberg */
  335. #define RND_FSCMYK      2   /** CMYK-specialized 32Bit Floyd-Steinberg */
  336. "FSCMYK32",                 /** CMYK-specialized 32Bit Floyd-Steinberg */
  337. #define RND_FSCMY_K     3   /** CMY_K Rendering */
  338. "FSCMY_K",
  339. NULL
  340. };
  341.  
  342. static const char *const upd_format[] = { "upOutputFormat",
  343. #define FMT_RAS         1   /** Generates SUN-Rasterfiles */
  344. "SunRaster",                /** Generates SUN-Rasterfiles */
  345. #define FMT_EPSON       2   /** Generates X+Y-Weaved ESC/P-Output */
  346. "Epson",                    /** Generates X+Y-Weaved ESC/P-Output */
  347. #define FMT_ESCP2Y      3   /** Generates Y-Weaved ESC/P2-Output */
  348. "EscP2",                    /** Generates Y-Weaved ESC/P2-Output */
  349. #define FMT_ESCP2XY     4   /** Generates X+Y-Weaved ESC/P2-Output */
  350. "EscP2XY",                  /** Generates X+Y-Weaved ESC/P2-Output */
  351. #define FMT_RTL         5   /** Generates HP-PCL/RTL-Output */
  352. "Pcl",                      /** Generates HP-PCL/RTL-Output */
  353. #define FMT_CANON       6   /** Generates Output for Canon extended mode (hr) */
  354. "Canon",                    /** Generates Output for Canon extended mode (hr) */
  355. NULL
  356. };
  357.  
  358. static const char *const *const upd_choice[] = {
  359. #define C_MAPPER        0   /** the selected Mapper */
  360.    upd_mapper,
  361. #define C_RENDER        1   /** the selected Rendering */
  362.    upd_render,
  363. #define C_FORMAT        2   /** the selected Choice */
  364.    upd_format
  365. };
  366.  
  367. /** Names for the flags (bool)
  368. */
  369.  
  370. static const char *const upd_flags[] = {      /** */
  371. #define B_REVDIR            ((uint32) 1<<0)   /** FS-Dir-Flag */
  372. "upFSReverseDirection",                       /** FS-Dir-Flag */
  373. #define B_FIXDIR            ((uint32) 1<<1)   /** Do not alter FS-direction */
  374. "upFSFixedDirection",                         /** Do not alter FS-direction */
  375. #define B_FSWHITE           ((uint32) 1<<2)   /** Process white in FS */
  376. "upFSProcessWhiteSpace",                      /** Process white in FS */
  377. #define B_FSZERO            ((uint32) 1<<3)   /** Zero FS-Initialization */
  378. "upFSZeroInit",                               /** Zero FS-Initialization */
  379.  
  380. #define B_PAGEWIDTH         ((uint32) 1<<4)   /** Adjust Width in BOP */
  381. "upAdjustPageWidthCommand",                   /** Adjust Page-Width in BOP */
  382. #define B_PAGELENGTH        ((uint32) 1<<5)   /** Adjust Length in BOP */
  383. "upAdjustPageLengthCommand",                  /** Adjust Page-Length in BOP */
  384. #define B_TOPMARGIN         ((uint32) 1<<6)   /** Adjust Top-Margin in BOP */
  385. "upAdjustTopMarginCommand",                   /** Adjust Top-Margin in BOP */
  386. #define B_BOTTOMMARGIN      ((uint32) 1<<7)   /** Adjust Bottom-Margin in BOP */
  387. "upAdjustBottomMarginCommand",                /** Adjust Bottom-Margin in BOP */
  388. #define B_RESOLUTION        ((uint32) 1<<8)   /** Adjust Resolution in BOP */
  389. "upAdjustResolutionCommand",                  /** Adjust Resolution in BOP */
  390. #define B_MEDIASIZE         ((uint32) 1<<9)   /** Adjust Mediasize in BOP */
  391. "upAdjustMediaSize",                          /** Adjust Mediasize in BOP */
  392.  
  393. #define B_XABS              ((uint32) 1<<10)  /** Use Absolute X-Values */
  394. "upFormatXabsolute",                          /** Use Absolute X-Values */
  395. #define B_YABS              ((uint32) 1<<11)  /** Use Absolute Y-Values */
  396. "upFormatYabsolute",                          /** Use Absolute Y-Values */
  397.  
  398. #define B_MAP               ((uint32) 1<<12)  /** Mapping Initialized */
  399. "upColorModelInitialized",                    /** Mapping Initialized */
  400. #define B_BUF               ((uint32) 1<<13)  /** Raster-Buffer Initialized */
  401. "upRasterBufferInitialized",                  /** Raster-Buffer Initialized */
  402. #define B_RENDER            ((uint32) 1<<14)  /** Rendering Initialized */
  403. "upRenderingInitialized",                     /** Rendering Initialized */
  404. #define B_FORMAT            ((uint32) 1<<15)  /** Formatter Initialized */
  405. "upOutputFormatInitialized",                  /** Formatter Initialized */
  406. #define B_ABORT             ((uint32) 1<<16)  /** Abort on Interrupt */
  407. "upOutputAborted",                            /** Abort on Interrupt */
  408. #define B_ERROR             ((uint32) 1<<17)  /** Severe Error detected */
  409. "upErrorDetected",                            /** Severe Error detected */
  410.  
  411. #define B_OPEN              ((uint32) 1<<18)  /** Open-Command written */
  412. "upWroteData",                                /** Open-Command written */
  413.  
  414. #define B_YFLIP             ((uint32) 1<<19)  /** Mirrored printing (hr) */
  415. "upYFlip",                                    /** Mirrored printing (hr) */
  416.  
  417. #define B_REDUCEK           ((uint32) 1<<20)  /** CMY->Black Reduction */
  418. "upFSReduceK"
  419.  
  420. };
  421.  
  422. /** B_OK4GO: Bits required to execute the print-loop */
  423.  
  424. #define B_OK4GO  (B_MAP | B_BUF | B_RENDER | B_FORMAT)
  425.  
  426. /** Names for the ints
  427. */
  428.  
  429. static const char *const upd_ints[] = {
  430. #define I_PWIDTH            0                 /** Output-Width */
  431. "upOutputWidth",
  432. #define I_PHEIGHT           1                 /** Output-Height */
  433. "upOutputHeight",
  434. #define I_OCOMP             2                 /** Output-Components */
  435. "upOutputComponents",
  436. #define I_NSCNBUF           3                 /** Output-Buffers */
  437. "upOutputBuffers",
  438. #define I_XSTEP             4                 /** Unit-Step */
  439. "upOutputXStep", /* > 0 -> divide Raster-X, < 0 muliply Raster-X */
  440. #define I_XOFS              5                 /** abs. X-Offset */
  441. "upOutputXOffset",
  442. #define I_YSTEP             6                 /** Unit-Step */
  443. "upOutputYStep", /* > 0 -> divide Raster-Y, < 0 muliply Raster-Y */
  444. #define I_YOFS              7                 /** abs. Y-Offset */
  445. "upOutputYOffset",
  446. #define I_PINS2WRITE        8                 /** Number of Pins */
  447. "upOutputPins",
  448.  
  449. #define I_NXPASS            9                 /** X-Passes */
  450. "upWeaveXPasses",
  451. #define I_NYPASS           10                 /** Y-Passes */
  452. "upWeaveYPasses",
  453. #define I_NPASS            11                 /** Total # Passes */
  454. "upWeavePasses",
  455. #define I_BEG_Y            12                 /** Start of normal Weaving */
  456. "upWeaveInitialScan",
  457. #define I_END_Y            13                 /** End of normal Weaving */
  458. "upWeaveFinalScan",
  459. #define I_BEGSKIP          14                 /** A Scan-Offset */
  460. "upWeaveYOffset"
  461. };
  462.  
  463. /** Names for the Integer-Arrays
  464. */
  465.  
  466. static const char *const upd_int_a[] = {      /** */
  467. #define IA_COLOR_INFO       0                 /** external color_info */
  468. "upColorInfo",                                /** external color_info */
  469.  
  470. #define IA_COMPBITS         1                 /** Bits stored per Component */
  471. "upComponentBits",                            /** Bits stored per Component */
  472. #define IA_COMPSHIFT        2                 /** Shift for the stored Bits */
  473. "upComponentShift",                           /** Shift for the stored Bits */
  474. #define IA_COMPORDER        3                 /** Order of Output-Components */
  475. "upOutputComponentOrder",                     /** Order of Output-Components */
  476.  
  477. #define IA_STD_DY           4                 /** Standard-Weave Feeds */
  478. "upWeaveYFeeds",                              /** Standard-Weave Feeds */
  479. #define IA_STD_IX           5                 /** Standard-Weave X-Passes */
  480. "upWeaveXStarts",                             /** Standard-Weave X-Start */
  481. #define IA_BEG_DY           6                 /** Initial-Weave Feeds */
  482. "upWeaveInitialYFeeds",                       /** Initial-Weave Feeds */
  483. #define IA_BEG_IX           7                 /** Initial-Weave X-Start */
  484. "upWeaveInitialXStarts",                      /** Initial-Weave X-Start */
  485. #define IA_BEGBOT           8                 /** Initial-Weave #Pins */
  486. "upWeaveInitialPins",                         /** Initial-Weave #Pins */
  487. #define IA_END_DY           9                 /** Final-Weave Feeds */
  488. "upWeaveFinalYFeeds",                         /** Final-Weave Feeds */
  489. #define IA_END_IX          10                 /** Final-Weave X-Start */
  490. "upWeaveFinalXStarts",                        /** Final-Weave X-Start */
  491. #define IA_ENDTOP          11                 /** Final-Weave #Pins */
  492. "upWeaveFinalPins"                            /** Final-Weave #Pins */
  493. };
  494.  
  495. /** Names of the String-Parameters
  496. */
  497.  
  498. static const char *const upd_strings[] = { /** */
  499. #define S_MODEL             0                 /** Name of the Printer-Model */
  500. "upModel",                                    /** Name of the Printer-Model */
  501. #define S_OPEN              1                 /** Printer-Begin-Job */
  502. "upBeginJobCommand",                          /** Printer-Begin-Job */
  503. #define S_CLOSE             2                 /** Printer-End-Job */
  504. "upEndJobCommand",                            /** Printer-End-Job */
  505. #define S_BEGIN             3                 /** Printer-Begin-Page */
  506. "upBeginPageCommand",                         /** Printer-Begin-Page */
  507. #define  S_END              4                 /** Printer-End-Page */
  508. "upEndPageCommand",                           /** Printer-End-Page */
  509. #define  S_ABORT            5                 /** Printer-Abort-Command */
  510. "upAbortCommand",                             /** Printer-Abort-Command */
  511.  
  512. #define S_XMOVE             6                 /** X-Positioning-Command */
  513. "upXMoveCommand",                             /** X-Positioning-Command */
  514. #define S_XSTEP             7                 /** X-Step Command (1<I_XSTEP) */
  515. "upXStepCommand",                             /** X-Step Command (1<I_XSTEP) */
  516. #define S_SETLF             8                 /** Set-Linefeed-Command */
  517. "upSetLineFeedCommand",                       /** Set-Linefeed-Command */
  518. #define S_YMOVE             9                 /** Y-Positioning-Command */
  519. "upYMoveCommand",                             /** Y-Positioning-Command */
  520. #define S_YSTEP            10                 /** Y-Step Command (1<I_YSTEP) */
  521. "upYStepCommand"                              /** Y-Step Command (1<I_YSTEP) */
  522. }; /** */
  523.  
  524. /** Names for the String-Arrays
  525. */
  526.  
  527. static const char *const upd_string_a[] = {   /** */
  528. #define SA_SETCOMP          0                 /** Select Components */
  529. "upSelectComponentCommands",                  /** Select Components */
  530. #define SA_WRITECOMP        1                 /** Write Component Comands */
  531. "upWriteComponentCommands"                    /** Write Component Commands */
  532. };                                            /** */
  533.  
  534. /** Names for the float-Arrays
  535. */
  536. static const char *const upd_float_a[] = {    /** */
  537. #define FA_WXFER            0                 /** White-Transfer */
  538. "upWhiteTransfer",                            /** White-Transfer */
  539. #define FA_RXFER            1                 /** Red-Transfer */
  540. "upRedTransfer",                              /** Red-Transfer */
  541. #define FA_GXFER            2                 /** Green-Transfer */
  542. "upGreenTransfer",                            /** Green-Transfer */
  543. #define FA_BXFER            3                 /** Blue-Transfer */
  544. "upBlueTransfer",                             /** Blue-Transfer */
  545. #define FA_KXFER            4                 /** Black-Transfer */
  546. "upBlackTransfer",                            /** Black-Transfer */
  547. #define FA_CXFER            5                 /** Cyan-Transfer */
  548. "upCyanTransfer",                             /** Cyan-Transfer */
  549. #define FA_MXFER            6                 /** Magenta-Transfer */
  550. "upMagentaTransfer",                          /** Magenta-Transfer */
  551. #define FA_YXFER            7                 /** Yellow-Transfer */
  552. "upYellowTransfer",                           /** Yellow-Transfer */
  553. #define FA_MARGINS          8                 /** private Margins */
  554. "upMargins",                                  /** private Margins */
  555. #define FA_MAP              9                 /** Color-Map       */
  556. "upColorMap"                                  /** Color-Map       */
  557. };                                            /** */
  558.  
  559. /* ------------------------------------------------------------------- */
  560. /* UPD-specific datatypes                                              */
  561. /* ------------------------------------------------------------------- */
  562.  
  563. /**
  564. int32 and uint32 are 32Bit-Integer-Types used in the
  565. Floyd-Steinberg Algorithm and instead of gx_color_index. The
  566. 8-Byte long's on some 64Bit-Machines are apparently useless,
  567. since gdevprn.c does (currently) support only 32-Bit Rasterdata.
  568. */
  569.  
  570. #if     arch_log2_sizeof_int < 2  /* int is too small */
  571.    typedef          long  int32;
  572. #define                   INT32_MIN  LONG_MIN
  573. #define                   INT32_MAX  LONG_MAX
  574.    typedef unsigned long uint32;
  575. #define                  UINT32_MAX ULONG_MAX
  576. #else                             /* int is sufficient */
  577.    typedef          int   int32;
  578. #define                   INT32_MIN   INT_MIN
  579. #define                   INT32_MAX   INT_MAX
  580.    typedef unsigned int  uint32;
  581. #define                  UINT32_MAX  UINT_MAX
  582. #endif                            /* use int or long ? */
  583.  
  584. /**
  585. "updcmap" is used by the color-mapping functions of the driver.
  586. there are four cmaps in the "uniprint"-structure, one for each component.
  587. To be exact, it's not "4" but rather "UPD_CMAP_MAX", which is a synonym.
  588. */
  589.  
  590. typedef struct updcmap_s { /** */
  591.    gx_color_value      *code;      /** Values related to codes */
  592.    uint32               bitmsk;    /** Mask, right justified */
  593.    int                  bitshf;    /** Shift to right-justify */
  594.    int                  xfer;      /** Index to the Xfer-Array */
  595.    int                  bits;      /** # of Bits */
  596.    int                  comp;      /** Output-Number */
  597.    bool                 rise;      /* Rising/Falling Curve */
  598. } updcmap_t, *updcmap_p;  /** */
  599. typedef const updcmap_t *updcmap_pc;
  600.  
  601.  
  602. /**
  603. "updcomp" holds similar informations, but is used for the rendering
  604. */
  605.  
  606. typedef struct updcomp_s {  /* Parameters for Floyd-Steinberg */
  607.    int32                offset;    /* Offset added to scaled values */
  608.    int32                scale;     /* Scale for the raw values */
  609.    int32                threshold; /* Val must be larger than this to fire */
  610.    int32                spotsize;  /* subtracted from Val when fired */
  611.    uint32               bitmsk;    /* Mask */
  612.    int                  bitshf;    /* shift */
  613.    int                  bits;      /* # of Bits */
  614.    int                  cmap;      /* Index for the Parameter-name */
  615. } updcomp_t, *updcomp_p;    /* Parameters for Floyd-Steinberg */
  616.  
  617. /** updscan is the Element of the scan-buffer. */
  618.  
  619. typedef struct updscan_s { /* Single Scanline (1 Bit/Pixel) */
  620.    byte   *bytes;      /* Buffer used w. 32-Bit Words */
  621.    int    *xbegin;     /* 1st  Pixel set (or nbytes<<3 if none) */
  622.    int    *xend;       /* last Pixel set (or -1, if none) */
  623. } updscan_t, *updscan_p;   /* Single Scanline (1 Bit/Pixel) */
  624.  
  625.  
  626. /** Main upd-Structure ***/
  627.  
  628. #define UPD_CMAP_MAX     4 /** Number of Colormaps provided */
  629. #define UPD_VALPTR_MAX  32 /** Number of valbuf-Pointers */
  630.  
  631. #define upd_proc_pxlget(name) uint32 name(P1(upd_p upd))
  632. #define upd_proc_render(name) int name(P1(upd_p upd))
  633. #define upd_proc_writer(name) int name(P2(upd_p upd,FILE *out))
  634.  
  635. struct upd_s { /* All upd-specific data */
  636.  
  637.    int                   *choice;     /** Named-Choices */
  638.    int                   *ints;       /** Integers */
  639.    gs_param_int_array    *int_a;      /** Integer-Arrays */
  640.    gs_param_string       *strings;    /** Strings */
  641.    gs_param_string_array *string_a;   /** String-Arrays */
  642.    gs_param_float_array  *float_a;    /** Float-Arrays */
  643.  
  644.    updcmap_t              cmap[UPD_CMAP_MAX]; /** Mapping-Data */
  645.  
  646.    byte                  *gsbuf;      /* Storage for GS-Rasterdata */
  647.    byte                  *gsscan;     /* Begin of GS-Rasterdata */
  648.  
  649.    byte                  *pxlptr;     /* Source for pxlget */
  650.    upd_proc_pxlget(     (*pxlget));   /* The Pixel-Reader */
  651.    upd_proc_render(     (*render));   /* Actual Rendering */
  652.    upd_proc_writer(     (*writer));
  653.  
  654.    updscan_p             *scnbuf;     /* Output-Values */
  655.    int32                 *valbuf;     /* Floyd-Steinberg-Buffer */
  656.    void                  *valptr[UPD_VALPTR_MAX];
  657.  
  658.    byte                  *outbuf;     /* Output-Buffer */
  659.    upd_proc_render(     (*start_render)); /* Setup for rendering */
  660.    upd_proc_writer(     (*start_writer)); /* Setup for writilg */
  661.  
  662.    uint32                 flags;      /** Some flags */
  663.    int                    pdwidth;    /** pdev-width upon open */
  664.    int                    pdheight;   /** pdev-height upon open */
  665.  
  666.    uint                   ngsbuf;     /* Size of gsbuf */
  667.    int                    gswidth;    /* Width in GS-Pixels */
  668.    int                    gsheight;   /* Height in GS-Pixels */
  669.  
  670.    int                    rwidth;     /* Rendering-Width */
  671.  
  672.    int                    pwidth;     /* Printing-Width */
  673.    int                    pheight;    /* # scanlines printed */
  674.  
  675.    int                    ncomp;      /* # Components in gsbuf */
  676.    int                    nmap;       /* # Entries in color-map */
  677.  
  678.    uint                   nvalbuf;    /* Size of valbuf */
  679.    int                    nscnbuf;    /* Number of entries in scnbuf. */
  680.  
  681.    int                    ocomp;      /* # Components written */
  682.    int                    nbytes;     /* Size of scnbuf[][].words */
  683.    int                    nlimits;    /* Size of scnbuf[][].xbegin/end */
  684.    int                    scnmsk;     /* Size of scanbuf - 1 */
  685.    uint                   noutbuf;    /* Size of the Output-Buffer */
  686.  
  687.    int                    ixpass;     /* Current X-pass (0 ... nxpass-1) */
  688.    int                    ipass;      /* Current pass (0 ... npass-1) */
  689.    int                    icomp;      /* Selected Component */
  690.    int                    lf;         /* Selected Line-Space */
  691.  
  692.    int                    xprinter;   /* Actual X-Position */
  693.  
  694.    int                    yscan;      /* Top-Scan (page-vari) */
  695.    int                    yprinter;   /* Actual Y-Position (page-vari) */
  696.    int                    yscnbuf;    /* Y not yet buffered */
  697. };             /* All upd-specific data */
  698.  
  699.  
  700. /* ------------------------------------------------------------------- */
  701. /* Various Message-Levels                                              */
  702. /* ------------------------------------------------------------------- */
  703.  
  704. /**
  705. UPD_MESSAGES, Is collection of Bits, that controls Messages
  706. */
  707.  
  708. #define UPD_M_NONE      0x0000 /** No Messages at all */
  709. #define UPD_M_ERROR     0x0001 /** Errors */
  710. #define UPD_M_WARNING   0x0002 /** Warnings */
  711. #define UPD_M_TOPCALLS  0x0004 /** Log Calls to main Functions */
  712. #define UPD_M_MAPCALLS  0x0008 /** Log Color-Mapping-Calls */
  713. #define UPD_M_SETUP     0x0010 /** Log Setup-Activity */
  714. #define UPD_M_FSBUF     0x0020 /** Error-Summary for valbuf */
  715.  
  716.  
  717. /* ------------------------------------------------------------------- */
  718. /* The UPD-Routines                                                    */
  719. /* ------------------------------------------------------------------- */
  720.  
  721. /**
  722. Besides the main routines required for the color-mapping, that were
  723. declared near the beginning, there are some auxillary functions.
  724. Most prominent are "upd_open_map" and "upd_close_map", which
  725. do the proper actions when opening and closing the device.
  726. */
  727.  
  728. private int             upd_open_map( P1(upd_device *udev));
  729. private int             upd_close_map(P1(upd_device *udev));
  730.  
  731. /**
  732. But "upd_truncate" and "upd_expand" are also mentionable. They are
  733. the actual workhorses for the component-oriented mapping. When mapping
  734. the 16Bit Component-Values to the indices, some truncation takes place
  735. and this is what "upd_truncate" does, in the most general manner i can
  736. think of and with O(log(n)) in time. "upd_expand" is required for the
  737. reverse mapping-functions and is a constant-time `algorithm'.
  738. */
  739. private uint32          upd_truncate(P3(upd_pc,int,gx_color_value));
  740. private gx_color_value  upd_expand(  P3(upd_pc,int,uint32));
  741.  
  742. /**
  743. The next group of internal functions adresses the rendering. Besides
  744. the main-functions "upd_open_render" and "upd_close_render", there
  745. are groups of up to 3 Functions, for each algorithm available with
  746. UPD. Two routines are invoked during open and close and the third
  747. is called for each scanline. Actually a fourth function is provided,
  748. that is invoked at the beginning of each page to be printed, but the
  749. current algorithms do not need it.
  750. */
  751. private void            upd_open_render(   P1(upd_device *udev));
  752. private void            upd_close_render(  P1(upd_device *udev));
  753.  
  754. private void            upd_open_fscomp(   P1(upd_device *udev));
  755. private int             upd_fscomp(        P1(upd_p upd));
  756. private void            upd_close_fscomp(  P1(upd_device *udev));
  757.  
  758. private void            upd_open_fscmyk(   P1(upd_device *udev));
  759. private int             upd_fscmyk(        P1(upd_p upd));
  760.  
  761. private void            upd_open_fscmy_k(  P1(upd_device *udev));
  762. private int             upd_fscmy_k(       P1(upd_p upd));
  763.  
  764. /**
  765. I hope that the formatting stuff can be kept simple and thus most
  766. of the work is done inside the general open and close-functions.
  767. During open, there is a call to a format-specific open-function, but
  768. this is only for checking and determining the amount of of bytes required
  769. for the output-buffer (and limit-values in the scan-buffer).
  770. */
  771. private int             upd_open_writer(   P1(upd_device *udev));
  772. private void            upd_close_writer(  P1(upd_device *udev));
  773. #if UPD_SIGNAL
  774. private void            upd_signal_handler(P1(int sig));
  775. #endif
  776.  
  777. /**
  778. The first format are the uncompressed! SUN-Rasterfiles. The primary intention
  779. of this format is testing, but it might turn out to be useful for other
  780. purposes, even if the amount of generated data is huge. On the other hand
  781. it is a violation of UPD's rules: the start-routine computes the Begin-Page
  782. sequence (the Rasterfile header) since it would be a nuisance to provide
  783. this code within each (test-)personalization in PostScript.
  784. */
  785. private int             upd_open_rascomp(   P1(upd_device *udev));
  786. private int             upd_start_rascomp(  P2(upd_p upd, FILE *out));
  787. private int             upd_rascomp(        P2(upd_p upd, FILE *out));
  788.  
  789. /**
  790. The second format is ESC/P, the format introduced with the first Epson
  791. impact printers. This format is used by a lot of other printers too.
  792. It is also uncompressed. This formatter supports X- and Y-Weaving,
  793. which makes it the most sophisticated one inside this driver.
  794. */
  795.  
  796. private void            upd_limits(        P2(upd_p upd, bool check));
  797. private int             upd_open_wrtescp(  P1(upd_device *udev));
  798. private int             upd_wrtescp(       P2(upd_p upd, FILE *out));
  799.  
  800. /**
  801. The third format is ESC/P2, the format use by the newer Epson-Printers.
  802. It allows runlength-Compression similar to the RTL/PCL-Family of Printers.
  803. This formatter does not allow for X-Weaving.
  804.  
  805. The fourth writer is a ESC/P2-Writer, that supports X-Weaving
  806. */
  807. private int             upd_rle(P3(byte *out,const byte *in,int nbytes));
  808. private int             upd_open_wrtescp2( P1(upd_device *udev));
  809. private int             upd_wrtescp2(      P2(upd_p upd, FILE *out));
  810. private int             upd_wrtescp2x(     P2(upd_p upd, FILE *out));
  811.  
  812. /**
  813. The fifth writer is a HP-RTL/PCL-Writer
  814. */
  815.  
  816. private int             upd_open_wrtrtl(   P1(upd_device *udev));
  817. private int             upd_wrtrtl(        P2(upd_p upd, FILE *out));
  818.  
  819. /**
  820. The sixth writer is for Canon Extended Mode (currently BJC610) (hr)
  821. */
  822.  
  823. private int             upd_open_wrtcanon( P1(upd_device *udev));
  824. private int             upd_wrtcanon(      P2(upd_p upd, FILE *out));
  825.  
  826. /**
  827. Generalized Pixel Get & Read
  828. */
  829. private uint32 upd_pxlfwd(P1(upd_p upd));
  830. private uint32 upd_pxlrev(P1(upd_p upd));
  831. #define upd_pxlget(UPD) (*UPD->pxlget)(UPD)
  832.  
  833.  
  834. /* ------------------------------------------------------------------- */
  835. /* Macros to deal with the Parameter-Memory                            */
  836. /* ------------------------------------------------------------------- */
  837.  
  838. /**
  839. Usually the creation of copies of external parameters is not necessary,
  840. at least with gs-versions > 4.03. But uniprint writes to the parameters
  841. in some cases or creates some by itself, thus to get a unified interface
  842. all parameter-data are copied and thus it is legal to manipulate them.
  843.  
  844. Here are several Macros, named "UPD_MM_*" to deal with that.
  845. */
  846.  
  847. /** UPD_MM_GET_ARRAY allocates & initializes an array of values */
  848. #define UPD_MM_GET_ARRAY(Which,Nelts)                                 \
  849.    Which = NULL;                                                      \
  850.    if(0 < (Nelts)) {                                                  \
  851.       byte *tmp = gs_malloc(Nelts,sizeof(Which[0]),"uniprint/params");\
  852.       if(tmp) {                                                       \
  853.          memset(tmp,0,(Nelts)*sizeof(Which[0]));                      \
  854.          Which = (void *) tmp;                                        \
  855.       } else {                                                        \
  856.           return_error(gs_error_VMerror);                             \
  857.       }                                                               \
  858.    }
  859.  
  860. /** UPD_MM_DEL_ARRAY frees an array of values */
  861. #define UPD_MM_DEL_ARRAY(Which,Nelts,Delete)                          \
  862.    if(Which && 0 < (Nelts)) {                                         \
  863.       uint ii;                                                        \
  864.       for(ii = 0; (Nelts) > ii; ++ii) Delete(Which[ii]);              \
  865.       gs_free((byte *)Which,Nelts,sizeof(Which[0]),"uniprint/params");\
  866.    }                                                                  \
  867.    Which = 0
  868.  
  869. /** UPD_MM_DEL_VALUE deletes a value, does nothing */
  870. #define UPD_MM_DEL_VALUE(Which) /* */
  871.  
  872. /** UPD_MM_DEL_PARAM deletes a single gs-array-parameter */
  873. #define UPD_MM_DEL_PARAM(Which)  {                                \
  874.    if(Which.data && Which.size)                                   \
  875.       gs_free((byte *)Which.data,Which.size,sizeof(Which.data[0]),\
  876.          "uniprint/params");                                      \
  877. }
  878.  
  879. /** UPD_MM_DEL_APARAM deletes a nested gs-array-parameter */
  880. #define UPD_MM_DEL_APARAM(Which) {                                \
  881.    if(Which.data && Which.size) {                                 \
  882.       uint iii;                                                   \
  883.       for(iii = 0; iii < Which.size; ++iii)                       \
  884.          UPD_MM_DEL_PARAM(Which.data[iii]);                       \
  885.       gs_free((byte *)Which.data,Which.size,sizeof(Which.data[0]),\
  886.          "uniprint/params");                                      \
  887.    }                                                              \
  888. }
  889.  
  890. /** UPD_MM_CPY_ARRAY creates a new copy of an array of values */
  891. #define UPD_MM_CPY_ARRAY(To,From,Nelts,Copy)                \
  892.    UPD_MM_GET_ARRAY(To,Nelts);                              \
  893.    if(To && From) {                                         \
  894.       uint ii;                                              \
  895.       for(ii = 0; (Nelts) > ii; ++ii) Copy(To[ii],From[ii]);\
  896.    }
  897.  
  898. /** UPD_MM_CPY_VALUE Copies a simple Value */
  899. #define UPD_MM_CPY_VALUE(To,From)  To = From
  900.  
  901. /** UPD_MM_CPY_PARAM Creates a copy of a gs-parameter */
  902. #define UPD_MM_CPY_PARAM(To,From)                                     \
  903.    if(From.data && From.size) {                                       \
  904.       UPD_MM_GET_ARRAY(To.data,From.size);                            \
  905.       if(To.data) {                                                   \
  906.          To.size = From.size;                                         \
  907.          memcpy((byte *)To.data,From.data,To.size*sizeof(To.data[0]));\
  908.       }                                                               \
  909.    }
  910.  
  911. /** UPD_MM_CPY_APARAM Creates a copy of a nested gs-parameter */
  912. #define UPD_MM_CPY_APARAM(To,From)                                    \
  913.    if(From.data && From.size) {                                       \
  914.       UPD_MM_GET_ARRAY(To.data,From.size);                            \
  915.       if(To.data) {                                                   \
  916.          gs_param_string *tmp2 = (gs_param_string *) To.data;         \
  917.          uint iii;                                                    \
  918.          To.size = From.size;                                         \
  919.          for(iii = 0; To.size > iii; ++iii)                           \
  920.             UPD_MM_CPY_PARAM(tmp2[iii],From.data[iii]);               \
  921.       }                                                               \
  922.    }
  923.  
  924. /* ------------------------------------------------------------------- */
  925. /* UPD-Initialized-Data                                                */
  926. /* ------------------------------------------------------------------- */
  927.  
  928. /** Version-String */
  929.  
  930. static const char rcsid[] = "$Revision: 1.3 $";
  931.  
  932. /** Default-Transfer-curve */
  933.  
  934. static const float upd_data_xfer[2] = { 0.0, 1.0 };
  935.  
  936. /*@ > */
  937.  
  938.  
  939. /* ------------------------------------------------------------------- */
  940. /* upd_signal_handler: Catch interrupts                                */
  941. /* ------------------------------------------------------------------- */
  942.  
  943. #if UPD_SIGNAL
  944. static upd_p sigupd = NULL;
  945. private void
  946. upd_signal_handler(int sig)
  947. {
  948.   if(sigupd) sigupd->flags |= B_ABORT;
  949. }
  950. #endif
  951.  
  952.  
  953. /* ------------------------------------------------------------------- */
  954. /* upd_print_page: The main workhorse                                  */
  955. /* ------------------------------------------------------------------- */
  956.  
  957. /**
  958. Function: upd_print_page
  959.  
  960. This is the top-level printing routine. It works through this
  961. steps:
  962.  
  963.  1. Once for each generated file, the "device-open-sequence" is written.
  964.  2. The "page-begin-sequence" is written.
  965.  
  966.  3. The data are generated and written:
  967.     3.1: Data are converted into a "printer-family"-specific format.
  968.          This step includes the halftoning, if selected.
  969.     3.2: Data are written with a printer-specific function.
  970.          There is not much code-compression inside theese functions,
  971.          since i observed to improvments in print-speed. Other
  972.          drivers do a better job in this.
  973.  
  974.  4. The "page-end-sequence" is written.
  975.  5. If a one-page-per-file mode is selected, the "device-close-sequence"
  976.     is added to the output. For multi-page files, this writing is
  977.     performed in "upd_close", the drivers close-function.
  978.  
  979. The routine is quite short, since all the allocation and checking
  980. occur in upd_open and upd_putparams. The only test, that upd_print_page
  981. does, is the verification wether the device is in a sane state. This
  982. must be done here, since during the initialisation, the device is
  983. usually opened several times, before obtaining a valid state.
  984. */
  985.  
  986. private int
  987. upd_print_page(gx_device_printer *pdev, FILE *out)
  988. {
  989.    upd_device *const udev  = (upd_device *) pdev;
  990.    const upd_p       upd   = udev->upd;
  991.    const int *const  ints  = upd ? upd->ints : NULL;
  992.    int error,need,yfill;
  993.  
  994. #if UPD_SIGNAL /* variables required for signal-handling only */
  995.    void (*oldint )(P1(int)) = NULL;
  996.    void (*oldterm)(P1(int)) = NULL;
  997.    upd_p  oldupd            = sigupd;
  998. #endif         /* variables required for signal-handling only */
  999.  
  1000. /*
  1001.  * Refuse to work, if not explicitly enabled during open
  1002.  * (some/lot of allocated memory is required)
  1003.  */
  1004.    if(!upd || B_OK4GO != (upd->flags & (B_OK4GO | B_ERROR))) {
  1005. #if UPD_MESSAGES & (UPD_M_ERROR | UPD_M_TOPCALLS)
  1006.          fprintf(stderr,"CALL-REJECTED upd_print_page(0x%05lx,0x%05lx)\n",
  1007.              (long) udev,(long) out);
  1008. #endif
  1009.       return gs_error_undefined;
  1010.    }
  1011.  
  1012. #if UPD_MESSAGES & UPD_M_TOPCALLS
  1013.    fprintf(stderr,"CALL: upd_print_page(0x%05lx,0x%05lx)\n",
  1014.       (long) udev,(long) out);
  1015. #endif
  1016.  
  1017. #if UPD_SIGNAL /* Setup of signal-handling */
  1018.    sigupd  = upd;
  1019.    oldint  = signal(SIGINT, upd_signal_handler);
  1020.    oldterm = signal(SIGTERM,upd_signal_handler);
  1021. #endif         /* Setup of signal-handling */
  1022.  
  1023. /*
  1024.  * If the OutputFile was just opened, transfer the Open-Sequence to it.
  1025.  */
  1026.    if(!(upd->flags & B_OPEN)) {
  1027.  
  1028.       if(0   <  upd->strings[S_OPEN].size)
  1029.          fwrite(upd->strings[S_OPEN].data,1,upd->strings[S_OPEN].size,out);
  1030.       upd->flags |= B_OPEN;
  1031.    }
  1032. /*
  1033.  * Always write the the Page-begin-sequence
  1034.  */
  1035.    if(0  <   upd->strings[S_BEGIN].size)
  1036.       fwrite(upd->strings[S_BEGIN].data,1,upd->strings[S_BEGIN].size,out);
  1037. /*
  1038.  * Establish page-variables
  1039.  */
  1040.  
  1041. /* Positions */
  1042.    upd->xprinter  = 0;
  1043.    upd->yscan     = 0; /* Position we are processing */
  1044.    upd->yprinter  = 0; /* Actual Printer-Positions */
  1045.    upd->yscnbuf   = 0; /* Next free scnbuf-Line */
  1046.  
  1047. /* Rendering & Writing Setup, if available */
  1048.    if(upd->start_render) (*upd->start_render)(upd);
  1049.    if(upd->start_writer) (*upd->start_writer)(upd,out);
  1050.  
  1051. /* How many scanlines do we need ? */
  1052.    need = ints[I_NYPASS] * ints[I_PINS2WRITE];
  1053.    if(0 >= need) need = 1;
  1054.  
  1055. /* The Weave-counters */
  1056.    upd->ipass  =  0;
  1057.    upd->ixpass =  0;
  1058.    upd->icomp  = -1; /* Enforces initial selection */
  1059.    upd->lf     = -1; /* Enforces initial selection */
  1060. /*
  1061.  * Main Loop
  1062.  */
  1063.    while(upd->pheight > upd->yscan) { /* Main-Loop */
  1064.  
  1065. /*
  1066.  *    Load as much data into the scan-buffer as possible
  1067.  *    (this is done in scan-sequence, the printing not necessarily.)
  1068.  */
  1069.       if(ints[I_BEGSKIP] > upd->yscan) yfill = 0;
  1070.       else                             yfill = upd->yscan - ints[I_BEGSKIP];
  1071.  
  1072.       for(yfill += upd->nscnbuf; upd->yscnbuf < yfill; upd->yscnbuf++) {
  1073.  
  1074.          if(upd->gsheight > upd->yscnbuf)  {
  1075.  
  1076.             if(0 > (*dev_proc(udev,get_bits))((gx_device *) udev,
  1077.                                    upd->yscnbuf,upd->gsbuf,&upd->gsscan)) {
  1078. #if UPD_MESSAGES & UPD_M_WARNING
  1079.                fprintf(stderr,"get_bits aborted with error, yscnbuf = %4d\n",
  1080.                   upd->yscnbuf);
  1081. #endif
  1082.                break;
  1083.             }
  1084.          } else {
  1085.  
  1086.             memset(upd->gsscan = upd->gsbuf,0,upd->ngsbuf);
  1087.  
  1088.          }
  1089.  
  1090.          if(0 > (*upd->render)(upd)) {
  1091. #if UPD_MESSAGES & UPD_M_WARNING
  1092.             fprintf(stderr,"Rendering aborted with error, yscnbuf = %4d\n",
  1093.                upd->yscnbuf);
  1094. #endif
  1095.             break;
  1096.          }
  1097.  
  1098.       }
  1099. /*
  1100.  *    Did the buffering loop take an error exit ?
  1101.  */
  1102.       if((upd->yscnbuf ^ yfill) & upd->scnmsk) break;
  1103. /*
  1104.  *    Print as much as possible
  1105.  */
  1106.       while((upd->yscan - ints[I_BEGSKIP] + need) < upd->yscnbuf) {
  1107.  
  1108. /*        first write the scan(s) */
  1109.           (*upd->writer)(upd,out);
  1110.  
  1111. /*        Check for termination */
  1112.           if(upd->yscan >= upd->pheight) break;
  1113.           if(upd->flags  & B_ABORT ) {
  1114. #if UPD_MESSAGES & UPD_M_WARNING
  1115.              fprintf(stderr,"Printing aborted upon interrupt, yscan = %4d\n",
  1116.                 upd->yscan);
  1117. #endif
  1118.              break;
  1119.           }
  1120.       }
  1121. /*
  1122.  *    Did the print-Loop take an error exit ?
  1123.  */
  1124.       if((upd->yscan - ints[I_BEGSKIP] + need) < upd->yscnbuf) break;
  1125.    }                                  /* Main-Loop */
  1126.  
  1127. /*
  1128.  * If we aborted for some reason, use the dedicated sequence
  1129.  */
  1130.  
  1131.    if((upd->pheight > upd->yscan) &&
  1132.       (0  <  upd->strings[S_ABORT].size)) { /* Only This! */
  1133.       fwrite(upd->strings[S_ABORT].data,1,upd->strings[S_ABORT].size,out);
  1134.  
  1135.       upd->flags &= ~B_OPEN; /* Inhibit Close-Sequence ! */
  1136. /*
  1137.  * If there is no special sequence, or we came to normal end,
  1138.  * write the normal sequence, if any
  1139.  */
  1140.  
  1141.    } else if(0  <   upd->strings[S_END].size) {
  1142.       fwrite(upd->strings[S_END].data,1,upd->strings[S_END].size,out);
  1143.    }
  1144. /*
  1145.  * If necessary, write the close-sequence
  1146.  */
  1147.    if((NULL != udev->fname  ) && strchr(udev->fname,'%')) {
  1148.  
  1149.       if(0  <   upd->strings[S_CLOSE].size)
  1150.          fwrite(upd->strings[S_CLOSE].data,1,upd->strings[S_CLOSE].size,out);
  1151.  
  1152.       upd->flags &= ~B_OPEN;
  1153.    }
  1154.  
  1155. /*
  1156.  * clean up, and return status
  1157.  */
  1158.  
  1159.    fflush(out); /* just to prepare for ferror */
  1160.  
  1161.    if(upd->pheight > upd->yscan) error = gs_error_interrupt;
  1162.    else if(ferror(out))          error = gs_error_ioerror;
  1163.    else                          error = 0;
  1164.  
  1165. #if UPD_MESSAGES & UPD_M_TOPCALLS
  1166.    fprintf(stderr,"RETURN: %d = upd_print_page(0x%05lx,0x%05lx)\n",
  1167.       error,(long) udev,(long)out);
  1168. #endif
  1169.  
  1170. #if UPD_SIGNAL /* Restore Interrupt-state */
  1171.       sigupd = oldupd;
  1172.       (void) signal(SIGINT ,oldint);
  1173.       (void) signal(SIGTERM,oldterm);
  1174. #endif         /* Restore Interrupt-state */
  1175.  
  1176.    return error;
  1177. }
  1178.  
  1179. /* ------------------------------------------------------------------- */
  1180. /* upd_open: Initialize everything for printing                        */
  1181. /* ------------------------------------------------------------------- */
  1182. /**
  1183. "upd_open" is -through the specified table of procedures- called instead
  1184. of the normal open-procedures for printer-devices, that performs quite
  1185. a complex job. Thus it is necessary to call this  `superclass-open´
  1186. here.
  1187.  
  1188. Besides that, this routine does quite a complex job too, in initializes
  1189. everything required to print a page. This might be time-consuming, the
  1190. alternative would be "upd_print_page", but i often print 100 pages or
  1191. more, but i never experienced more than 5-6 open-calls.
  1192. */
  1193.  
  1194. private int
  1195. upd_open(gx_device *pdev)
  1196. {
  1197.    upd_device *const udev    =  (upd_device *) pdev;
  1198.    const upd_p       upd     =  udev->upd;
  1199.    int              error;
  1200.  
  1201. #if UPD_MESSAGES & UPD_M_TOPCALLS
  1202.       fprintf(stderr,"CALL: upd_open(0x%05lx)\n",(long) pdev);
  1203. #endif
  1204.  
  1205. /** enforce the UPD-Margins */
  1206.  
  1207.    if((NULL != upd) && 
  1208.       (NULL != upd->float_a[FA_MARGINS].data) &&
  1209.       (4    == upd->float_a[FA_MARGINS].size)    ) {
  1210.       static float m[4];
  1211.       m[1] = upd->float_a[FA_MARGINS].data[1] / 72.0;
  1212.       m[3] = upd->float_a[FA_MARGINS].data[3] / 72.0;
  1213.       if(B_YFLIP & upd->flags) {
  1214.          m[0] = upd->float_a[FA_MARGINS].data[2] / 72.0;
  1215.          m[2] = upd->float_a[FA_MARGINS].data[0] / 72.0;
  1216.       } else {
  1217.          m[0] = upd->float_a[FA_MARGINS].data[0] / 72.0;
  1218.          m[2] = upd->float_a[FA_MARGINS].data[2] / 72.0;
  1219.       }
  1220.       gx_device_set_margins((gx_device *) udev, m, true);
  1221.    }
  1222.  
  1223. /** call the super-class open **/
  1224.    error = gdev_prn_open(pdev);
  1225.  
  1226. /** invoke the subroutines, if an upd is present. */
  1227.  
  1228.    if(upd) {
  1229.  
  1230.       upd->flags &= ~B_OK4GO;
  1231.  
  1232. /**
  1233. The following initializations are run, even in case of an error in
  1234. the super-class open, just to bring our upd into a sane state.
  1235. */
  1236.       if(0 > error) upd->flags |= B_ERROR;
  1237.  
  1238.       if(gs_error_VMerror == upd_open_map(udev)) error = gs_error_VMerror;
  1239.  
  1240. /**
  1241. The following piece of code is here for demonstration-purposes:
  1242. It determines the size of the printed image and allocates the
  1243. buffer for the raw raster-data
  1244. */
  1245.       upd->gswidth  = udev->width -
  1246.          (dev_l_margin(udev)+dev_r_margin(udev))*udev->x_pixels_per_inch;
  1247.  
  1248.       upd->gsheight = udev->height -
  1249.          (dev_t_margin(udev)+dev_b_margin(udev))*udev->y_pixels_per_inch;
  1250.  
  1251.       upd->ngsbuf = 0;    /* Ensure sane values */
  1252.       upd->gsbuf  = NULL; /* Ensure sane values */
  1253.  
  1254.       if(B_MAP & upd->flags) { /* Only if prerequisites were met */
  1255.          uint want  = gx_device_raster(pdev,true);
  1256.          upd->gsbuf = gs_malloc(want,1,"upd/gsbuf");
  1257.  
  1258.          if(upd->gsbuf) {
  1259.             upd->ngsbuf = want;
  1260.             upd->flags |= B_BUF;  /* Signal Success */
  1261.          } else {
  1262.             error = gs_error_VMerror; /* Signal Error */
  1263.             upd->flags |= B_ERROR;
  1264.          }
  1265.  
  1266.       }                            /* Only if prerequisites were met */
  1267.  
  1268.       upd_open_render(udev);  /* First subloop in printing */
  1269.  
  1270.       if(gs_error_VMerror == upd_open_writer(udev)) error = gs_error_VMerror;
  1271.  
  1272.       udev->upd->pdwidth  = udev->width;
  1273.       udev->upd->pdheight = udev->height;
  1274.  
  1275. #if UPD_MESSAGES & UPD_M_SETUP
  1276.       if((upd->flags & (B_OK4GO | B_ERROR)) == B_OK4GO) {
  1277.         int i,j,l,ln,lv;
  1278.         fprintf(stderr,"\nupd->flags    = 0x%05lx\n",(unsigned long)upd->flags);
  1279.         fprintf(stderr,  "upd->pdwidth  = %5d\n",upd->pdwidth);
  1280.         fprintf(stderr,  "upd->pdheight = %5d\n",upd->pdheight);
  1281.         fprintf(stderr,  "upd->ngsbuf   = %5u\n",upd->ngsbuf);
  1282.         fprintf(stderr,  "upd->gswidth  = %5d\n",upd->gswidth);
  1283.         fprintf(stderr,  "upd->gsheight = %5d\n",upd->gsheight);
  1284.         fprintf(stderr,  "upd->rwidth   = %5d\n",upd->rwidth);
  1285.         fprintf(stderr,  "upd->pwidth   = %5d\n",upd->pwidth);
  1286.         fprintf(stderr,  "upd->pheight  = %5d\n",upd->pheight);
  1287.         fprintf(stderr,  "upd->nvalbuf  = %5u\n",upd->nvalbuf);
  1288.         fprintf(stderr,  "upd->nscnbuf  = %5d\n",upd->nscnbuf);
  1289.         fprintf(stderr,  "upd->ncomp    = %5d\n",upd->ncomp);
  1290.         fprintf(stderr,  "upd->ocomp    = %5d\n",upd->ocomp);
  1291.         fprintf(stderr,  "upd->nbytes   = %5d\n",upd->nbytes);
  1292.         fprintf(stderr,  "upd->nlimits  = %5d\n",upd->nlimits);
  1293.         fprintf(stderr,  "upd->scnmsk   = %5d\n",upd->scnmsk);
  1294.         fprintf(stderr,  "upd->noutbuf  = %5u\n",upd->noutbuf);
  1295.         fprintf(stderr,  "upd->ixpass   = %5d\n",upd->ixpass);
  1296.         fprintf(stderr,  "upd->ipass    = %5d\n",upd->ipass);
  1297.         fprintf(stderr,  "upd->icomp    = %5d\n",upd->icomp);
  1298.         fprintf(stderr,  "upd->lf       = %5d\n",upd->lf);
  1299.         fprintf(stderr,  "upd->xprinter = %5d\n",upd->xprinter);
  1300.         fprintf(stderr,  "upd->yscan    = %5d\n",upd->yscan);
  1301.         fprintf(stderr,  "upd->yprinter = %5d\n",upd->yprinter);
  1302.         fprintf(stderr,  "upd->yscnbuf  = %5d\n",upd->yscnbuf);
  1303.  
  1304.         ln = 13;
  1305.         lv = 5;
  1306.         for(i = 0; countof(upd_choice) > i; ++i) {
  1307.           if(!upd_choice[i]) continue;
  1308.           l = strlen(upd_choice[i][0]);
  1309.           if(ln < l) ln = l; 
  1310.           for(j = 1; upd_choice[i][j]; ++j) {
  1311.             l = strlen(upd_choice[i][j]);
  1312.             if(lv < l) lv = l;
  1313.           }
  1314.         }
  1315.  
  1316.         for(i = 0; countof(upd_flags) > i; ++i) {
  1317.           if(upd_flags[i]) {
  1318.             l = strlen(upd_flags[i]);
  1319.             if(ln < l) ln = l;
  1320.           }
  1321.         }
  1322.  
  1323.         for(i = 0; countof(upd_ints) > i; ++i) {
  1324.           if(upd_ints[i]) {
  1325.             l = strlen(upd_ints[i]);
  1326.             if(ln < l) ln = l;
  1327.           }
  1328.         }
  1329.  
  1330.         for(i = 0; countof(upd_int_a) > i; ++i) {
  1331.           if(upd_int_a[i]) {
  1332.             l = strlen(upd_int_a[i]);
  1333.             if(ln < l) ln = l;
  1334.           }
  1335.         }
  1336.  
  1337.         for(i = 0; countof(upd_strings) > i; ++i) {
  1338.           if(upd_strings[i]) {
  1339.             l = strlen(upd_strings[i]);
  1340.             if(ln < l) ln = l;
  1341.           }
  1342.         }
  1343.  
  1344.         for(i = 0; countof(upd_string_a) > i; ++i) {
  1345.           if(upd_string_a[i]) {
  1346.             l = strlen(upd_string_a[i]);
  1347.             if(ln < l) ln = l;
  1348.           }
  1349.         }
  1350.  
  1351.         for(i = 0; countof(upd_float_a) > i; ++i) {
  1352.           if(upd_float_a[i]) {
  1353.             l = strlen(upd_float_a[i]);
  1354.             if(ln < l) ln = l;
  1355.           }
  1356.         }
  1357.  
  1358.         for(i = 0; countof(upd_choice) > i; ++i) {
  1359.           if(upd_choice[i]) {
  1360.             fprintf(stderr,"%*s = %-*s (%2d)\n",ln,upd_choice[i][0],
  1361.                lv,upd_choice[i][upd->choice[i]],upd->choice[i]);
  1362.           } else {
  1363.             fprintf(stderr,"%*s[%2d] = %2d\n",ln-4,"upd_choice",i,
  1364.                upd->choice[i]);
  1365.           }
  1366.         }
  1367.  
  1368.         for(i = 0; countof(upd_flags) > i; ++i) {
  1369.           if(upd_flags[i]) {
  1370.             fprintf(stderr,"%*s = %s\n",ln,upd_flags[i],
  1371.                ((uint32) 1 << i) & upd->flags ? "true" : "false");
  1372.           } else {
  1373.             fprintf(stderr,"%*s[%2d] = %s\n",ln-4,"upd_flags",i,
  1374.                ((uint32) 1 << i) & upd->flags ? "true" : "false");
  1375.  
  1376.           }
  1377.         }
  1378.  
  1379.         for(i = 0; countof(upd_ints) > i; ++i) {
  1380.           if(upd_ints[i]) {
  1381.             fprintf(stderr,"%*s = %5d\n",ln,upd_ints[i],upd->ints[i]);
  1382.           } else {
  1383.             fprintf(stderr,"%*s[%2d] = %5d\n",ln-4,"upd_ints",i,upd->ints[i]);
  1384.           }
  1385.         }
  1386.  
  1387.       }
  1388.  
  1389.  
  1390.       fprintf(stderr,"\n%sready to print\n\n",
  1391.          B_OK4GO != (upd->flags & (B_OK4GO | B_ERROR)) ?
  1392.          "NOT " : "");
  1393. #endif
  1394.  
  1395.    }
  1396.  
  1397. #if UPD_MESSAGES & UPD_M_TOPCALLS
  1398.       fprintf(stderr,"RETURN: %d = upd_open(0x%05lx)\n",
  1399.          error,(long) pdev);
  1400. #endif
  1401.  
  1402.    return error;
  1403. }
  1404.  
  1405. /* ------------------------------------------------------------------- */
  1406. /* upd_close: Release everything allocated in upd_open                 */
  1407. /* ------------------------------------------------------------------- */
  1408.  
  1409. private int
  1410. upd_close(gx_device *pdev)
  1411. {
  1412.    upd_device *const udev    =  (upd_device *) pdev;
  1413.    const upd_p       upd     =  udev->upd;
  1414.    int         error = 0;
  1415.    int         code;
  1416.  
  1417. #if UPD_MESSAGES & UPD_M_TOPCALLS
  1418.    fprintf(stderr,"CALL: upd_close(0x%05lx)\n",(long)pdev);
  1419. #endif
  1420.  
  1421. /** If necessary, write the close-sequence **/
  1422.  
  1423.    if( upd && (( B_OPEN | B_OK4GO) ==
  1424.                ((B_OPEN | B_OK4GO | B_ERROR) & upd->flags))) {
  1425.  
  1426.       if(udev->file && upd->strings && 0 < upd->strings[S_CLOSE].size)
  1427.          fwrite(upd->strings[S_CLOSE].data,1,
  1428.                 upd->strings[S_CLOSE].size,udev->file);
  1429.  
  1430.       upd->flags &= ~B_OPEN;
  1431.    }
  1432.  
  1433. /** Then release the open-allocated memory */
  1434.    if(upd) {
  1435.  
  1436.       upd_close_writer(udev);
  1437.  
  1438.       if(upd->gsbuf)
  1439.          gs_free(upd->gsbuf,upd->ngsbuf,1,"uniprint/gsbuf");
  1440.       upd->gsbuf  = NULL;
  1441.       upd->ngsbuf = 0;
  1442.       upd->flags &= ~B_BUF;
  1443.  
  1444.       upd_close_render(udev);
  1445.       upd_close_map(udev);
  1446.  
  1447.       UPD_MM_DEL_ARRAY(upd->choice,  countof(upd_choice),  UPD_MM_DEL_VALUE);
  1448.       UPD_MM_DEL_ARRAY(upd->ints,    countof(upd_ints),    UPD_MM_DEL_VALUE);
  1449.       UPD_MM_DEL_ARRAY(upd->int_a,   countof(upd_int_a),   UPD_MM_DEL_PARAM);
  1450.       UPD_MM_DEL_ARRAY(upd->strings, countof(upd_strings), UPD_MM_DEL_PARAM);
  1451.       UPD_MM_DEL_ARRAY(upd->string_a,countof(upd_string_a),UPD_MM_DEL_APARAM);
  1452.       UPD_MM_DEL_ARRAY(upd->float_a, countof(upd_float_a), UPD_MM_DEL_PARAM);
  1453.  
  1454.       gs_free(upd,sizeof(upd[0]),1,"uniprint");
  1455.  
  1456.       udev->upd = NULL;
  1457.    }
  1458.  
  1459. /** Then call the superclass close **/
  1460.    code = gdev_prn_close(pdev);
  1461.    error = error > code ? code : error;
  1462.  
  1463.  
  1464. #if UPD_MESSAGES & UPD_M_TOPCALLS
  1465.       fprintf(stderr,"RETURN: %d = upd_close(0x%05lx)\n",
  1466.          error,(long) pdev);
  1467. #endif
  1468.  
  1469.    return error;
  1470. }
  1471.  
  1472. /* ------------------------------------------------------------------- */
  1473. /* upd_get_params: Export Parameters to the Interpreter                */
  1474. /* ------------------------------------------------------------------- */
  1475.  
  1476. #if UPD_MESSAGES & UPD_M_TOPCALLS
  1477. #define UPD_EXIT_GET(Err,Dev,List)                                      \
  1478.    if(0 > Err) {                                                        \
  1479.       fprintf(stderr,"RETURN-%d: %d upd_get_params(0x%05lx,0x%05lx)\n", \
  1480.          __LINE__,Err,(long) Dev,(long) List);                          \
  1481.       return_error(Err);                                                \
  1482.    }
  1483. #else
  1484. #define UPD_EXIT_GET(Err,Dev,List) if(0 > Err) return_error(Err);
  1485. #endif
  1486.  
  1487. private int
  1488. upd_get_params(gx_device *pdev, gs_param_list *plist)
  1489. {
  1490.    upd_device *const udev    =  (upd_device *) pdev;
  1491.    const upd_p       upd     =  udev->upd;
  1492.    int               error,i;
  1493.  
  1494. #if UPD_MESSAGES & UPD_M_TOPCALLS
  1495.       fprintf(stderr,"CALL: upd_get_params(0x%05lx,0x%05lx)\n",
  1496.          (long) udev,(long) plist);
  1497. #endif
  1498.  
  1499. /** Call the SuperClass-get_params at the beginning */
  1500.    error = gdev_prn_get_params((gx_device *)udev,plist);
  1501.    UPD_EXIT_GET(error,udev,plist);
  1502.  
  1503. /** Export the version */
  1504.    if(upd_version) { /* Version-Export enabled */
  1505.       udev->upd_version.data       = (const byte *) rcsid;
  1506.       udev->upd_version.size       = strlen(rcsid);
  1507.       udev->upd_version.persistent = true;
  1508.       error = param_write_string(plist,upd_version,&udev->upd_version);
  1509.       UPD_EXIT_GET(error,udev,plist);
  1510.    }                 /* Version-Export enabled */
  1511.  
  1512. /** Export the Named choices */
  1513.    for(i = 0; i < countof(upd_choice); ++i) {
  1514.       if(!upd_choice[i]) continue; /* Choice-Export disabled */
  1515.       if(upd && upd->choice && upd->choice[i]) {
  1516.          gs_param_string name;
  1517.          name.data       = (const byte *) upd_choice[i][upd->choice[i]];
  1518.          name.size       = strlen((const char *) name.data);
  1519.          name.persistent = true;
  1520.          error = param_write_name(plist,upd_choice[i][0],&name);
  1521.       } else {
  1522.          error = param_write_null(plist,upd_choice[i][0]);
  1523.       }
  1524.       UPD_EXIT_GET(error,udev,plist);
  1525.    }
  1526.  
  1527. /** Export the flags (bool) */
  1528.    for(i = 0; i < countof(upd_flags); ++i) {
  1529.       if(!upd_flags[i]) continue; /* Flag-Export disabled */
  1530.       if(upd) {
  1531.          bool value = upd->flags & ((uint32) 1 << i);
  1532.          error = param_write_bool(plist,upd_flags[i],&value);
  1533.       } else {
  1534.          error = param_write_null(plist,upd_flags[i]);
  1535.       }
  1536.       UPD_EXIT_GET(error,udev,plist);
  1537.    }
  1538.  
  1539. /** Export the ints */
  1540.    for(i = 0; i < countof(upd_ints); ++i) {
  1541.       if(!upd_ints[i]) continue; /* int-Export disabled */
  1542.       if(upd && upd->ints && upd->ints[i]) {
  1543.          int value = upd->ints[i];
  1544.          error = param_write_int( plist,upd_ints[i],&value);
  1545.       } else {
  1546.          error = param_write_null(plist,upd_ints[i]);
  1547.       }
  1548.       UPD_EXIT_GET(error,udev,plist);
  1549.    }
  1550.  
  1551. /** Export the int-arrays */
  1552.    for(i = 0; i < countof(upd_int_a); ++i) {
  1553.       if(!upd_int_a[i]) continue; /* int-Array-Export disabled */
  1554.       if(upd && upd->int_a && upd->int_a[i].size) {
  1555.          error = param_write_int_array( plist,upd_int_a[i],(upd->int_a+i));
  1556.       } else {
  1557.          error = param_write_null(plist,upd_int_a[i]);
  1558.       }
  1559.       UPD_EXIT_GET(error,udev,plist);
  1560.    }
  1561.  
  1562. /** Export the strings */
  1563.    for(i = 0; i < countof(upd_strings); ++i) {
  1564.       if(!upd_strings[i]) continue; /* String-Export disabled */
  1565.       if(upd && upd->strings && upd->strings[i].size) {
  1566.          error = param_write_string( plist,upd_strings[i],(upd->strings+i));
  1567.       } else {
  1568.          error = param_write_null(plist,upd_strings[i]);
  1569.       }
  1570.       UPD_EXIT_GET(error,udev,plist);
  1571.    }
  1572.  
  1573. /** Export the string-Arrays */
  1574.    for(i = 0; i < countof(upd_string_a); ++i) {
  1575.       if(!upd_string_a[i]) continue; /* String-Array-Export disabled */
  1576.       if(upd && upd->string_a && upd->string_a[i].size) {
  1577.          error =
  1578.             param_write_string_array( plist,upd_string_a[i],(upd->string_a+i));
  1579.       } else {
  1580.          error = param_write_null(plist,upd_string_a[i]);
  1581.       }
  1582.       UPD_EXIT_GET(error,udev,plist);
  1583.    }
  1584.  
  1585. /** Export the float-Arrays */
  1586.    for(i = 0; i < countof(upd_float_a); ++i) {
  1587.       if(!upd_float_a[i]) continue; /* Float-Array-Export disabled */
  1588.       if(upd && upd->float_a && upd->float_a[i].size) {
  1589.          error =
  1590.             param_write_float_array( plist,upd_float_a[i],(upd->float_a+i));
  1591.       } else {
  1592.          error = param_write_null(plist,upd_float_a[i]);
  1593.       }
  1594.       UPD_EXIT_GET(error,udev,plist);
  1595.    }
  1596.  
  1597. #if UPD_MESSAGES & UPD_M_TOPCALLS
  1598.    fprintf(stderr,"RETURN: %d = upd_get_params(0x%05lx,0x%05lx)\n",
  1599.        error,(long) udev,(long) plist);
  1600. #endif
  1601.  
  1602.    return error;
  1603. }
  1604.  
  1605. #undef UPD_EXIT_GET
  1606.  
  1607. /* ------------------------------------------------------------------- */
  1608. /* upd_put_params: Load Parameters into the device-structure           */
  1609. /* ------------------------------------------------------------------- */
  1610.  
  1611. private int
  1612. upd_put_params(gx_device *pdev, gs_param_list *plist)
  1613. {
  1614.    upd_device *const      udev       = (upd_device *) pdev;
  1615.    upd_p                  upd        = udev->upd;
  1616.    int                    error      = 0, code,i;
  1617.  
  1618.    float                  MarginsHWResolution[2],Margins[2];
  1619.    gx_device_color_info   color_info;
  1620.    uint32                 flags      = 0;
  1621.    int                   *choice     = NULL;
  1622.    int                   *ints       = NULL;
  1623.    gs_param_int_array    *int_a      = NULL;
  1624.    gs_param_string       *strings    = NULL;
  1625.    gs_param_string_array *string_a   = NULL;
  1626.    gs_param_float_array  *float_a    = NULL, mfa;
  1627.  
  1628. /**
  1629. Error is used for two purposes: either it holds a negative error
  1630. code or it is used as a bitfield, that tells, which parameters
  1631. were actually loaded.  If any of the important parameters changed
  1632. upd_put_params closes the device, since the real parameter-evaluation
  1633. is carried out by upd_open.
  1634. */
  1635.  
  1636. #define UPD_PUT_FLAGS       0x0002
  1637. #define UPD_PUT_CHOICE      0x0004
  1638. #define UPD_PUT_INTS        0x0008
  1639. #define UPD_PUT_INT_A       0x0010
  1640. #define UPD_PUT_STRINGS     0x0020
  1641. #define UPD_PUT_STRING_A    0x0040
  1642. #define UPD_PUT_FLOAT_A     0x0080
  1643. #define UPD_PUT_CHANGEDSIZE 0x0100
  1644.  
  1645. #if UPD_MESSAGES & UPD_M_TOPCALLS
  1646.       fprintf(stderr,"CALL: upd_put_params(0x%05lx,0x%05lx)\n",
  1647.          (long)udev,(long)plist);
  1648. #endif
  1649.  
  1650.  
  1651. /**
  1652. I consider the following part of upd_put_params a bad-nasty-hack-hack
  1653. and i am uncertain, wether it really works in the intended way. I provide it
  1654. just for the case someone is performing nasty-parameter-changes on the
  1655. active device, especially switching the OutputFile. If this happens in
  1656. a situation, where data were written to the file, but the termination
  1657. sequence is required, the driver does it now. (If you want to know, why
  1658. i am writing bad-nasty-hack-hack, visit http://www.zark.com )
  1659. */
  1660.    if(upd && (B_OPEN & udev->upd->flags) && (NULL != udev->file)) {
  1661.  
  1662.       gs_param_string fname = { NULL, 0, false };
  1663.  
  1664.       code = param_read_string(plist,"OutputFile",&fname);
  1665.       if((1 != code) && (0 != code)) {
  1666.          code = param_read_null(plist,"OutputFile");
  1667.          if(0 == code) {
  1668.             fname.data = (const byte *) "";
  1669.             fname.size = 0;
  1670.          }
  1671.       }
  1672.  
  1673.       if((0 == code) && 
  1674.          strncmp((const char *)fname.data,udev->fname,fname.size)) {
  1675.          if(upd->strings && 0 < udev->upd->strings[S_CLOSE].size)
  1676.             fwrite(upd->strings[S_CLOSE].data,1,
  1677.                    upd->strings[S_CLOSE].size,udev->file);
  1678.  
  1679.          upd->flags &= ~B_OPEN;
  1680.       }
  1681.    }
  1682. /* Done with the bad-nasty-hack-hack */
  1683.  
  1684. /**
  1685. The next thing "upd_put_params" does, is a little strange too. It imports
  1686. a readonly-parameter, the version-string. I do not know wether it is still
  1687. required, but some versions of GHOSTSCRIPT disliked it very much, if an
  1688. existing parameter was not touched by the put-operation.
  1689.  
  1690. On the other hand it is the right time to show the basic-outline of the
  1691. parameter-importing flow. Basically the proper "param_read"-procedure
  1692. is called. If it indicated, that the parameter was present, but of the
  1693. wrong type, a read for the null-type is attempted, which is by convention
  1694. somehow an reset to default. This sequence is applied to all the parameters
  1695. and in case of the array-parameters, a succesful null-read is marked by
  1696. setting data and size to 0.
  1697. */
  1698. #if UPD_MESSAGES & UPD_M_SETUP
  1699. #define UPD_PARAM_READ(Param_read,Name,Object)       \
  1700.    code = Param_read(plist,Name,&Object);            \
  1701.    if(0 > code) {                                    \
  1702.       code = param_read_null(plist,Name);            \
  1703.       if(0 == code) memset(&Object,0,sizeof(Object));\
  1704.    }                                                 \
  1705.    if(!code) fprintf(stderr,                         \
  1706.       "upd_put_params: retrieved parameter \"%s\"\n",\
  1707.       Name);                                         \
  1708.    if(0 > code) {                                    \
  1709.       param_signal_error(plist,Name,code);           \
  1710.       if(error > code) error = code;                 \
  1711.    }
  1712. #else
  1713. #define UPD_PARAM_READ(Param_read,Name,Object)       \
  1714.    code = Param_read(plist,Name,&Object);            \
  1715.    if(0 > code) {                                    \
  1716.       code = param_read_null(plist,Name);            \
  1717.       if(0 == code) memset(&Object,0,sizeof(Object));\
  1718.    }                                                 \
  1719.    if(0 > code) {                                    \
  1720.       param_signal_error(plist,Name,code);           \
  1721.       if(error > code) error = code;                 \
  1722.    }
  1723. #endif
  1724.  
  1725.    UPD_PARAM_READ(param_read_string,upd_version,udev->upd_version)
  1726.  
  1727.  
  1728. /**
  1729. upd_put_params begins it's normal work by creating a copy, of
  1730. the data, that it might change, except for color_info that might
  1731. be changed in the device-structure, all manipulations are carried
  1732. out on this copies.
  1733. */
  1734.    MarginsHWResolution[0] = udev->MarginsHWResolution[0];
  1735.    MarginsHWResolution[1] = udev->MarginsHWResolution[1];
  1736.                Margins[0] = udev->Margins[0];
  1737.                Margins[1] = udev->Margins[1];
  1738.  
  1739.    color_info = udev->color_info;
  1740.    if(upd) {
  1741.      flags = upd->flags;
  1742.      UPD_MM_CPY_ARRAY(choice,  upd->choice,  countof(upd_choice),
  1743.         UPD_MM_CPY_VALUE);
  1744.      UPD_MM_CPY_ARRAY(ints,    upd->ints,    countof(upd_ints),
  1745.         UPD_MM_CPY_VALUE);
  1746.      UPD_MM_CPY_ARRAY(int_a,   upd->int_a,   countof(upd_int_a),
  1747.         UPD_MM_CPY_PARAM);
  1748.      UPD_MM_CPY_ARRAY(strings, upd->strings, countof(upd_strings),
  1749.         UPD_MM_CPY_PARAM);
  1750.      UPD_MM_CPY_ARRAY(string_a,upd->string_a,countof(upd_string_a),
  1751.         UPD_MM_CPY_APARAM);
  1752.      UPD_MM_CPY_ARRAY(float_a, upd->float_a, countof(upd_float_a),
  1753.         UPD_MM_CPY_PARAM);
  1754.    } else {
  1755.      flags = 0;
  1756.      UPD_MM_GET_ARRAY(choice,  countof(upd_choice));
  1757.      UPD_MM_GET_ARRAY(ints,    countof(upd_ints));
  1758.      UPD_MM_GET_ARRAY(int_a,   countof(upd_int_a));
  1759.      UPD_MM_GET_ARRAY(strings, countof(upd_strings));
  1760.      UPD_MM_GET_ARRAY(string_a,countof(upd_string_a));
  1761.      UPD_MM_GET_ARRAY(float_a, countof(upd_float_a));
  1762.    }
  1763.  
  1764. /** Import the Multiple-Choices */
  1765.    for(i = 0; countof(upd_choice) > i; ++i) {
  1766.       gs_param_string value = { NULL, 0, false};
  1767.       if(!upd_choice[i][0]) continue;
  1768.       UPD_PARAM_READ(param_read_name,upd_choice[i][0],value);
  1769.       if(0 == code) {
  1770.          if(0 <= error) error |= UPD_PUT_CHOICE;
  1771.          choice[i] = 0;
  1772.          if(0 < value.size) {
  1773.             int j;
  1774.             for(j = 1; upd_choice[i][j]; ++j) {
  1775.                if((strlen(upd_choice[i][j]) == value.size) &&
  1776.                   (0 == strncmp(upd_choice[i][j],
  1777.                                 (char *) value.data,value.size))) {
  1778.                   choice[i] = j;
  1779.                   break;
  1780.                }
  1781.             }
  1782.          }
  1783.       }
  1784.    }
  1785.  
  1786. /** Import the Boolean Values */
  1787.    for(i = 0; countof(upd_flags) > i; ++i) {
  1788.       uint32 bit  = (uint32) 1 << i;
  1789.       bool   flag = flags & bit ? true : false;
  1790.       if(!upd_flags[i]) continue;
  1791.       UPD_PARAM_READ(param_read_bool,upd_flags[i],flag);
  1792.       if(0 == code) {
  1793.          if(0 <= error) error |= UPD_PUT_FLAGS;
  1794.          if(flag) flags |=  bit;
  1795.          else     flags &= ~bit;
  1796.       }
  1797.    }
  1798.  
  1799. /** Import the Integer Values */
  1800.    for(i = 0; countof(upd_ints) > i; ++i) {
  1801.       int value = ints[i];
  1802.       if(!upd_ints[i]) continue;
  1803.       UPD_PARAM_READ(param_read_int,upd_ints[i],value);
  1804.       if(0 == code) {
  1805.          if(0 <= error) error |= UPD_PUT_INTS;
  1806.          ints[i] = value;
  1807.       }
  1808.    }
  1809.  
  1810. /** Import the Integer Arrays */
  1811.    for(i = 0; countof(upd_int_a) > i; ++i) {
  1812.       gs_param_int_array value = int_a[i];
  1813.       if(!upd_int_a[i]) continue;
  1814.       UPD_PARAM_READ(param_read_int_array,upd_int_a[i],value);
  1815.       if(0 == code) {
  1816.          if(0 <= error) error |= UPD_PUT_INT_A;
  1817.          UPD_MM_DEL_PARAM(int_a[i]);
  1818.          if(!value.size) {
  1819.             value.data = NULL;
  1820.             int_a[i]   = value;
  1821.          } else {
  1822.             UPD_MM_CPY_PARAM(int_a[i],value);
  1823.          }
  1824.       }
  1825.    }
  1826.  
  1827. /** Import the Strings */
  1828.    for(i = 0; countof(upd_strings) > i; ++i) {
  1829.       gs_param_string value = strings[i];
  1830.       if(!upd_strings[i]) continue;
  1831.       UPD_PARAM_READ(param_read_string,upd_strings[i],value);
  1832.       if(0 == code) {
  1833.          if(0 <= error) error |= UPD_PUT_STRINGS;
  1834.          UPD_MM_DEL_PARAM(strings[i]);
  1835.          if(!value.size) {
  1836.             value.data = NULL;
  1837.             strings[i]   = value;
  1838.          } else {
  1839.             UPD_MM_CPY_PARAM(strings[i],value);
  1840.          }
  1841.       }
  1842.    }
  1843.  
  1844. /** Import the String Arrays */
  1845.    for(i = 0; countof(upd_string_a) > i; ++i) {
  1846.       gs_param_string_array value = string_a[i];
  1847.       if(!upd_string_a[i]) continue;
  1848.       UPD_PARAM_READ(param_read_string_array,upd_string_a[i],value);
  1849.       if(0 == code) {
  1850.          if(0 <= error) error |= UPD_PUT_STRING_A;
  1851.          UPD_MM_DEL_APARAM(string_a[i]);
  1852.          if(!value.size) {
  1853.             value.data  = NULL;
  1854.             string_a[i] = value;
  1855.          } else {
  1856.             UPD_MM_CPY_APARAM(string_a[i],value);
  1857.          }
  1858.       }
  1859.    }
  1860.  
  1861. /** Import the Float Arrays */
  1862.    for(i = 0; countof(upd_float_a) > i; ++i) {
  1863.       gs_param_float_array value = float_a[i];
  1864.       if(!upd_float_a[i]) continue;
  1865.       UPD_PARAM_READ(param_read_float_array,upd_float_a[i],value);
  1866.       if(0 == code) {
  1867.          if(0 <= error) error |= UPD_PUT_FLOAT_A;
  1868.          UPD_MM_DEL_PARAM(float_a[i]);
  1869.          if(!value.size) {
  1870.             value.data = NULL;
  1871.             float_a[i] = value;
  1872.          } else {
  1873.             UPD_MM_CPY_PARAM(float_a[i],value);
  1874.          }
  1875.       }
  1876.    }
  1877.  
  1878. /**
  1879. Prior to the call to the superclass-put_params, the memory-layout and
  1880. the color-model needs adjustment. This is performed here, if any parameters
  1881. were set.
  1882. In addition to that, Resolution & Margin-Parameters are tested & adjusted.
  1883. */
  1884.    if(0 < error) {
  1885.  
  1886.       int *ip,*ip2,ncomp,nbits;
  1887.  
  1888.       if(6 > int_a[IA_COLOR_INFO].size) {
  1889.          UPD_MM_DEL_PARAM(int_a[IA_COLOR_INFO]);
  1890.          UPD_MM_GET_ARRAY(int_a[IA_COLOR_INFO].data,6);
  1891.          int_a[IA_COLOR_INFO].size = 6;
  1892.       }
  1893.       ip = (int *) int_a[IA_COLOR_INFO].data;
  1894.  
  1895.       if(0 == ip[0]) { /* Try to obtain num_components */
  1896.          switch(choice[C_MAPPER]) {
  1897.             case MAP_GRAY:     ip[0] = 1; break;
  1898.             case MAP_RGBW:     ip[0] = 3; break;
  1899.             case MAP_RGB:      ip[0] = 3; break;
  1900.             case MAP_CMYK:     ip[0] = 4; break;
  1901.             case MAP_CMYKGEN:  ip[0] = 4; break;
  1902.             case MAP_RGBOV:    ip[0] = 3; break;
  1903.             case MAP_RGBNOV:   ip[0] = 3; break;
  1904.             default:           ip[0] = color_info.num_components; break;
  1905.          }
  1906.       }                /* Try to obtain num_components */
  1907.  
  1908.       switch(choice[C_MAPPER]) {
  1909.          case MAP_GRAY:     ncomp = 1; break;
  1910.          case MAP_RGBW:     ncomp = 4; break;
  1911.          case MAP_RGB:      ncomp = 3; break;
  1912.          case MAP_CMYK:     ncomp = 4; break;
  1913.          case MAP_CMYKGEN:  ncomp = 4; break;
  1914.          case MAP_RGBOV:    ncomp = 4; break;
  1915.          case MAP_RGBNOV:   ncomp = 4; break;
  1916.          default:           ncomp = ip[0]; break;
  1917.       }
  1918.       if(UPD_CMAP_MAX < ncomp) ncomp = UPD_CMAP_MAX;
  1919.  
  1920.       if(ncomp > int_a[IA_COMPBITS].size) { /* Default ComponentBits */
  1921.          UPD_MM_GET_ARRAY(ip2,ncomp);
  1922.          nbits = 32 / ncomp;
  1923.          if(8 < nbits) nbits = 8;
  1924.          for(i = 0; i < ncomp; ++i) ip2[i] = nbits;
  1925.          UPD_MM_DEL_PARAM(int_a[IA_COMPBITS]);
  1926.          int_a[IA_COMPBITS].data = ip2;
  1927.          int_a[IA_COMPBITS].size = ncomp;
  1928.       }                                     /* Default ComponentBits */
  1929.  
  1930.       if(ncomp > int_a[IA_COMPSHIFT].size) {  /* Default ComponentShift */
  1931.          nbits = 0;
  1932.          for(i = 0; i < ncomp; ++i) nbits += int_a[IA_COMPBITS].data[i];
  1933.          UPD_MM_GET_ARRAY(ip2,ncomp);
  1934.          for(i = 0; i < ncomp; ++i) {
  1935.             ip2[i] = nbits - int_a[IA_COMPBITS].data[i];
  1936.             nbits -= int_a[IA_COMPBITS].data[i];
  1937.          }
  1938.          UPD_MM_DEL_PARAM(int_a[IA_COMPSHIFT]);
  1939.          int_a[IA_COMPSHIFT].data = ip2;
  1940.          int_a[IA_COMPSHIFT].size = ncomp;
  1941.       }                                       /* Default ComponentShift */
  1942.  
  1943.       if(0 == ip[1]) { /* Try to compute the depth */
  1944.          nbits = 0;
  1945.          for(i = 0; i < ncomp; ++i) {
  1946.             if(nbits < (int_a[IA_COMPBITS].data[i] +
  1947.                         int_a[IA_COMPSHIFT].data[i]))
  1948.                nbits =  int_a[IA_COMPBITS].data[i] +
  1949.                         int_a[IA_COMPSHIFT].data[i];
  1950.          }
  1951.          if(      1 >= nbits) nbits =  1;
  1952.          else if( 2 >= nbits) nbits =  2;
  1953.          else if( 4 >= nbits) nbits =  4;
  1954.          else if( 8 >= nbits) nbits =  8;
  1955.          else if(16 >= nbits) nbits = 16;
  1956.          else if(24 >= nbits) nbits = 24;
  1957.          else                 nbits = 32;
  1958.  
  1959.          ip[1] = nbits;
  1960.  
  1961.       }                /* Try to compute the depth */
  1962.  
  1963.       if(0 == ip[2]) { /* Number of Gray-Levels */
  1964.          nbits = 0;
  1965.          for(i = 0; i < ncomp; ++i) if(nbits < int_a[IA_COMPBITS].data[i])
  1966.             nbits = int_a[IA_COMPBITS].data[i];
  1967.          if(nbits > 8) nbits = 8;
  1968.          ip[2] = (1 << nbits) - 1;
  1969.       }                /* Number of Gray-Levels */
  1970.  
  1971.       if(0 == ip[3] && 1 < ip[0]) { /* Number of Colors */
  1972.          nbits = 0;
  1973.          for(i = 0; i < ip[0]; ++i) nbits += int_a[IA_COMPBITS].data[i];
  1974.          if(nbits > 8) nbits = 8;
  1975.          ip[3] = (1 << nbits) - 1;
  1976.       }                             /* Number of Colors */
  1977.  
  1978.       if(0 == ip[4]) { /* Gray-Ramp */
  1979.          nbits = 0;
  1980.          for(i = 0; i < ncomp; ++i) if(nbits < int_a[IA_COMPBITS].data[i])
  1981.             nbits = int_a[IA_COMPBITS].data[i];
  1982.          if(2 < nbits) ip[4] = 5;
  1983.          else          ip[4] = 2;
  1984.       }                /* Gray-Ramp */
  1985.  
  1986.       if(0 == ip[5] && 1 < ip[0]) { /* Color-Ramp */
  1987.          nbits = 0;
  1988.          for(i = 0; i < ncomp; ++i) if(nbits < int_a[IA_COMPBITS].data[i])
  1989.             nbits = int_a[IA_COMPBITS].data[i];
  1990.          if(2 < nbits) ip[5] = 5;
  1991.          else          ip[5] = 2;
  1992.       }                             /* Color-Ramp */
  1993.  
  1994.       udev->color_info.num_components = ip[0];
  1995.       udev->color_info.depth          = ip[1];
  1996.       udev->color_info.max_gray       = (gx_color_value) ip[2];
  1997.       udev->color_info.max_color      = (gx_color_value) ip[3];
  1998.       udev->color_info.dither_grays   = (gx_color_value) ip[4];
  1999.       udev->color_info.dither_colors  = (gx_color_value) ip[5];
  2000.  
  2001. /*
  2002.  * Now we're dealing with the Resolution- & Margin-Stuff
  2003.  * (This is close to be a bad-nasty-hack-hack)
  2004.  */
  2005.       if((0 == param_read_float_array(plist,"HWResolution",&mfa)) &&
  2006.          (2 == mfa.size) && (0 != mfa.data)) {
  2007.          udev->MarginsHWResolution[0] = mfa.data[0];
  2008.          udev->MarginsHWResolution[1] = mfa.data[1];
  2009.       } else {
  2010.          udev->MarginsHWResolution[0] = udev->HWResolution[0];
  2011.          udev->MarginsHWResolution[1] = udev->HWResolution[1];
  2012.       }
  2013.  
  2014.       if((0 == param_read_float_array(plist,".HWMargins",&mfa)) &&
  2015.          (4 == mfa.size) && (0 != mfa.data)) {
  2016.          udev->Margins[0] = -mfa.data[0] * udev->MarginsHWResolution[0] / 72.0;
  2017.          udev->Margins[1] = -mfa.data[3] * udev->MarginsHWResolution[1] / 72.0;
  2018.       }
  2019.    }                                       /* Change the color-Info */
  2020.  
  2021. /* Call the superclass-put_params now */
  2022.    code = gdev_prn_put_params((gx_device *)udev,plist);
  2023.    if(0 > code) error = code;
  2024.  
  2025. /**
  2026. If the superclass-"put_params" went o.k. too, then the new parameters are
  2027. transferred into the device-structure. In the case of "uniprint", this may
  2028.  
  2029.  1. Close the device, which might fail.
  2030.  2. Allocate new memory for the upd-specific structure, that might fail too.
  2031.  
  2032. */
  2033. /* *HGS* recognize a changed device geometry */
  2034.    if( udev->upd &&                              /* HGS */
  2035.       ((udev->width  != udev->upd->pdwidth) ||   /* HGS */
  2036.       (udev->height != udev->upd->pdheight)  ))  /* HGS */
  2037.         error |= UPD_PUT_CHANGEDSIZE;            /* HGS */
  2038.  
  2039.    if(0 < error && udev->is_open) {
  2040.       code = gs_closedevice((gx_device *)udev);
  2041.       if(0 > code) error = code;
  2042.    }
  2043.  
  2044.    if(0 < error) { /* Actually something loaded without error */
  2045.  
  2046.       if(!(upd = udev->upd)) {
  2047.         UPD_MM_GET_ARRAY(udev->upd,1);
  2048.         upd = udev->upd;
  2049.       } else {
  2050.         UPD_MM_DEL_ARRAY(upd->choice,  countof(upd_choice),  UPD_MM_DEL_VALUE);
  2051.         UPD_MM_DEL_ARRAY(upd->ints,    countof(upd_ints),    UPD_MM_DEL_VALUE);
  2052.         UPD_MM_DEL_ARRAY(upd->int_a,   countof(upd_int_a),   UPD_MM_DEL_PARAM);
  2053.         UPD_MM_DEL_ARRAY(upd->strings, countof(upd_strings), UPD_MM_DEL_PARAM);
  2054.         UPD_MM_DEL_ARRAY(upd->string_a,countof(upd_string_a),UPD_MM_DEL_APARAM);
  2055.         UPD_MM_DEL_ARRAY(upd->float_a, countof(upd_float_a), UPD_MM_DEL_PARAM);
  2056.       }
  2057.  
  2058.       upd->choice   = choice;
  2059.       upd->flags    = flags;
  2060.       upd->ints     = ints;
  2061.       upd->int_a    = int_a;
  2062.       upd->strings  = strings;
  2063.       upd->string_a = string_a;
  2064.       upd->float_a  = float_a;
  2065.  
  2066.       if(0 < error) error = 0;
  2067.  
  2068.    } else {
  2069.  
  2070.                   udev->Margins[0] =             Margins[0];
  2071.                   udev->Margins[1] =             Margins[1];
  2072.       udev->MarginsHWResolution[0] = MarginsHWResolution[0];
  2073.       udev->MarginsHWResolution[1] = MarginsHWResolution[1];
  2074.  
  2075.       udev->color_info = color_info;
  2076.       UPD_MM_DEL_ARRAY(choice,  countof(upd_choice),  UPD_MM_DEL_VALUE);
  2077.       UPD_MM_DEL_ARRAY(ints,    countof(upd_ints),    UPD_MM_DEL_VALUE);
  2078.       UPD_MM_DEL_ARRAY(int_a,   countof(upd_int_a),   UPD_MM_DEL_PARAM);
  2079.       UPD_MM_DEL_ARRAY(strings, countof(upd_strings), UPD_MM_DEL_PARAM);
  2080.       UPD_MM_DEL_ARRAY(string_a,countof(upd_string_a),UPD_MM_DEL_APARAM);
  2081.       UPD_MM_DEL_ARRAY(float_a, countof(upd_float_a), UPD_MM_DEL_PARAM);
  2082.  
  2083.    }
  2084.  
  2085. /*
  2086.  * upd_put_params keeps the Procedures upd to date
  2087.  */
  2088.  
  2089.    upd_procs_map(udev);
  2090.  
  2091.  
  2092. #if UPD_MESSAGES & UPD_M_TOPCALLS
  2093.       fprintf(stderr,"RETURN: %d = upd_put_params(0x%05lx,0x%05lx)\n",
  2094.          error,(long) udev, (long) plist);
  2095. #endif
  2096.  
  2097.    return error;
  2098. }
  2099.  
  2100. /* ------------------------------------------------------------------- */
  2101. /* upd_cmyk_icolor: KCMY->KCMY-Index Mapping                           */
  2102. /* ------------------------------------------------------------------- */
  2103. /**
  2104. The next Routines, that follow, are the color-mapping routines.
  2105. GHOSTSCRIPT talks about "gx_color_values" and the driver has
  2106. to merge the 1, 3 or four values into up to 32 Bits, that means
  2107. it is necessary to do some truncation and shifting. For the truncation
  2108. "uniprint" uses the internal function "upd_truncate" and "upd_expand"
  2109. reverses this in the reverse-mapping procedures.
  2110. */
  2111.  
  2112. private gx_color_index
  2113. upd_cmyk_icolor(gx_device *pdev,
  2114.    gx_color_value c, gx_color_value m, gx_color_value y,gx_color_value k)
  2115. {
  2116.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2117.    gx_color_index  rv;
  2118.  
  2119. /**
  2120. All 4-Component-Modi have to deal with the Problem, that a value
  2121. with all bits set can be produced, which is treated as an error-return
  2122. from the mapping-functions. But with RGBW or KCMY, there is a neat
  2123. trick: Grayscale are transferred as RGB/CMY=0 and holding Data only
  2124. in the W- or K-Component.
  2125. */
  2126.  
  2127.    if((c == m) && (m == y)) {
  2128.  
  2129.       rv = upd_truncate(upd,0,c > k ? c : k);
  2130.  
  2131.    } else {
  2132.  
  2133.       rv  = upd_truncate(upd,0,k) | upd_truncate(upd,1,c)
  2134.           | upd_truncate(upd,2,m) | upd_truncate(upd,3,y);
  2135.  
  2136.  
  2137. /* It might still become a "gx_no_color_value" due to truncation, thus: */
  2138.  
  2139.       if(rv == gx_no_color_index) rv ^= 1;
  2140.    }
  2141.  
  2142.  
  2143. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2144.   fprintf(stderr,
  2145. "cmyk_icolor: (%5.1f,%5.1f,%5.1f,%5.1f) : (%5.1f,%5.1f,%5.1f,%5.1f) : 0x%0*lx\n",
  2146.    255.0 * (double) c / (double) gx_max_color_value,
  2147.    255.0 * (double) m / (double) gx_max_color_value,
  2148.    255.0 * (double) y / (double) gx_max_color_value,
  2149.    255.0 * (double) k / (double) gx_max_color_value,
  2150.    255.0 * (double) ((rv >> upd->cmap[1].bitshf) & upd->cmap[1].bitmsk)
  2151.                  / (double) upd->cmap[1].bitmsk,
  2152.    255.0 * (double) ((rv >> upd->cmap[2].bitshf) & upd->cmap[2].bitmsk)
  2153.                  / (double) upd->cmap[2].bitmsk,
  2154.    255.0 * (double) ((rv >> upd->cmap[3].bitshf) & upd->cmap[3].bitmsk)
  2155.                  / (double) upd->cmap[3].bitmsk,
  2156.    255.0 * (double) ((rv >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2157.                  / (double) upd->cmap[0].bitmsk,
  2158.    (pdev->color_info.depth + 3)>>2,rv);
  2159. #endif
  2160.  
  2161.    return rv;
  2162. }
  2163.  
  2164. /* ------------------------------------------------------------------- */
  2165. /* upd_icolor_rgb: Stored KCMY back to a RGB                           */
  2166. /* ------------------------------------------------------------------- */
  2167.  
  2168. private int
  2169. upd_icolor_rgb(gx_device *pdev, gx_color_index color, gx_color_value prgb[3])
  2170. {
  2171.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2172.    gx_color_value c,m,y,k;
  2173.  
  2174. /*
  2175.  * Expand to the Component-Values
  2176.  */
  2177.    k = upd_expand(upd,0,color);
  2178.    c = upd_expand(upd,1,color);
  2179.    m = upd_expand(upd,2,color);
  2180.    y = upd_expand(upd,3,color);
  2181.  
  2182. /*
  2183.  * Then Invert and subtract K from the colors
  2184.  */
  2185.    prgb[0] = gx_max_color_value - c;
  2186.    if(prgb[0] > k) prgb[0] -= k;
  2187.    else            prgb[0]  = 0;
  2188.  
  2189.    prgb[1] = gx_max_color_value - m;
  2190.    if(prgb[1] > k) prgb[1] -= k;
  2191.    else            prgb[1]  = 0;
  2192.  
  2193.    prgb[2] = gx_max_color_value - y;
  2194.    if(prgb[2] > k) prgb[2] -= k;
  2195.    else            prgb[2]  = 0;
  2196.  
  2197.  
  2198. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2199.    fprintf(stderr,
  2200.     "icolor_rgb: 0x%0*lx -> (%5.1f,%5.1f,%5.1f,%5.1f) -> (%5.1f,%5.1f,%5.1f,%5.1f) -> (%5.1f,%5.1f,%5.1f)\n",
  2201.     (pdev->color_info.depth + 3)>>2,color,
  2202.     255.0 * (double) ((color >> upd->cmap[1].bitshf) & upd->cmap[1].bitmsk)
  2203.                      / (double) upd->cmap[1].bitmsk,
  2204.     255.0 * (double) ((color >> upd->cmap[2].bitshf) & upd->cmap[2].bitmsk)
  2205.                      / (double) upd->cmap[2].bitmsk,
  2206.     255.0 * (double) ((color >> upd->cmap[3].bitshf) & upd->cmap[3].bitmsk)
  2207.                      / (double) upd->cmap[3].bitmsk,
  2208.     255.0 * (double) ((color >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2209.                      / (double) upd->cmap[0].bitmsk,
  2210.     255.0 * (double)   c     / (double) gx_max_color_value,
  2211.     255.0 * (double)   m     / (double) gx_max_color_value,
  2212.     255.0 * (double)   y     / (double) gx_max_color_value,
  2213.     255.0 * (double)   k     / (double) gx_max_color_value,
  2214.     255.0 * (double) prgb[0] / (double) gx_max_color_value,
  2215.     255.0 * (double) prgb[1] / (double) gx_max_color_value,
  2216.     255.0 * (double) prgb[2] / (double) gx_max_color_value);
  2217. #endif
  2218.  
  2219.    return 0;
  2220. }
  2221.  
  2222. /* ------------------------------------------------------------------- */
  2223. /* upd_rgb_1color: Grayscale-RGB->Grayscale-index-Mapping              */
  2224. /* ------------------------------------------------------------------- */
  2225.  
  2226. private gx_color_index
  2227. upd_rgb_1color(gx_device *pdev,
  2228.    gx_color_value r, gx_color_value g, gx_color_value b)
  2229. {
  2230.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2231.    gx_color_index  rv;
  2232.  
  2233.    rv = upd_truncate(upd,0,r);
  2234.  
  2235. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2236.    fprintf(stderr,
  2237.       "rgb_1color: (%5.1f,%5.1f,%5.1f) : (%5.1f) : 0x%0*lx\n",
  2238.       255.0 * (double) r  / (double) gx_max_color_value,
  2239.       255.0 * (double) g  / (double) gx_max_color_value,
  2240.       255.0 * (double) b  / (double) gx_max_color_value,
  2241.       255.0 * (double) ((rv >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2242.                     / (double) upd->cmap[0].bitmsk,
  2243.       (pdev->color_info.depth + 3)>>2,rv);
  2244. #endif
  2245.  
  2246.    return rv;
  2247. }
  2248.  
  2249. /* ------------------------------------------------------------------- */
  2250. /* upd_1color_rgb: reversal of the above                               */
  2251. /* ------------------------------------------------------------------- */
  2252.  
  2253. private int
  2254. upd_1color_rgb(gx_device *pdev, gx_color_index color, gx_color_value prgb[3])
  2255. {
  2256.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2257. /*
  2258.  * Actual task: expand to full range of gx_color_value
  2259.  */
  2260.    prgb[0] = upd_expand(upd,0,color);
  2261.  
  2262.    prgb[2] = prgb[1] = prgb[0];
  2263.  
  2264. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2265.    fprintf(stderr,"1color_rgb: 0x%0*lx -> %5.1f -> (%5.1f,%5.1f,%5.1f)\n",
  2266.       (pdev->color_info.depth + 3)>>2,color,
  2267.       255.0 * (double) ((color >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2268.                        / (double) upd->cmap[0].bitmsk,
  2269.       255.0 * (double) prgb[0] / (double) gx_max_color_value,
  2270.       255.0 * (double) prgb[1] / (double) gx_max_color_value,
  2271.       255.0 * (double) prgb[2] / (double) gx_max_color_value);
  2272. #endif
  2273.  
  2274.    return 0;
  2275. }
  2276.  
  2277. /* ------------------------------------------------------------------- */
  2278. /* upd_rgb_3color: component-wise RGB->RGB-Mapping                     */
  2279. /* ------------------------------------------------------------------- */
  2280.  
  2281. private gx_color_index
  2282. upd_rgb_3color(gx_device *pdev,
  2283.    gx_color_value r, gx_color_value g, gx_color_value b)
  2284. {
  2285.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2286.    gx_color_index  rv;
  2287.  
  2288.    rv = upd_truncate(upd,0,r) | upd_truncate(upd,1,g) | upd_truncate(upd,2,b);
  2289.    if(rv == gx_no_color_index) rv ^= 1;
  2290.  
  2291. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2292.   fprintf(stderr,
  2293.    "rgb_3color: (%5.1f,%5.1f,%5.1f) : (%5.1f,%5.1f,%5.1f) : 0x%0*lx\n",
  2294.    255.0 * (double) r / (double) gx_max_color_value,
  2295.    255.0 * (double) g / (double) gx_max_color_value,
  2296.    255.0 * (double) b / (double) gx_max_color_value,
  2297.    255.0 * (double) ((rv >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2298.                  / (double) upd->cmap[0].bitmsk,
  2299.    255.0 * (double) ((rv >> upd->cmap[1].bitshf) & upd->cmap[1].bitmsk)
  2300.                  / (double) upd->cmap[1].bitmsk,
  2301.    255.0 * (double) ((rv >> upd->cmap[2].bitshf) & upd->cmap[2].bitmsk)
  2302.                  / (double) upd->cmap[2].bitmsk,
  2303.    (pdev->color_info.depth + 3)>>2,rv);
  2304. #endif
  2305.  
  2306.    return rv;
  2307. }
  2308.  
  2309. /* ------------------------------------------------------------------- */
  2310. /* upd_3color_rgb: reversal of the above                               */
  2311. /* ------------------------------------------------------------------- */
  2312.  
  2313. private int
  2314. upd_3color_rgb(gx_device *pdev, gx_color_index color, gx_color_value prgb[3])
  2315. {
  2316.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2317.  
  2318.    prgb[0] = upd_expand(upd,0,color);
  2319.    prgb[1] = upd_expand(upd,1,color);
  2320.    prgb[2] = upd_expand(upd,2,color);
  2321.  
  2322. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2323.    fprintf(stderr,
  2324.      "3color_rgb: 0x%0*lx -> (%5.1f,%5.1f,%5.1f) -> (%5.1f,%5.1f,%5.1f)\n",
  2325.       (pdev->color_info.depth + 3)>>2,color,
  2326.       255.0 * (double) ((color >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2327.                        / (double) upd->cmap[0].bitmsk,
  2328.       255.0 * (double) ((color >> upd->cmap[1].bitshf) & upd->cmap[1].bitmsk)
  2329.                        / (double) upd->cmap[1].bitmsk,
  2330.       255.0 * (double) ((color >> upd->cmap[2].bitshf) & upd->cmap[2].bitmsk)
  2331.                        / (double) upd->cmap[2].bitmsk,
  2332.  
  2333.       255.0 * (double) prgb[0] / (double) gx_max_color_value,
  2334.       255.0 * (double) prgb[1] / (double) gx_max_color_value,
  2335.       255.0 * (double) prgb[2] / (double) gx_max_color_value);
  2336. #endif
  2337.  
  2338.    return 0;
  2339. }
  2340.  
  2341. /* ------------------------------------------------------------------- */
  2342. /* upd_rgb_4color: Create an WRGB-Index from RGB                       */
  2343. /* ------------------------------------------------------------------- */
  2344.  
  2345. private gx_color_index
  2346. upd_rgb_4color(gx_device *pdev,
  2347.    gx_color_value r, gx_color_value g, gx_color_value b)
  2348. {
  2349.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2350.    gx_color_index  rv;
  2351.  
  2352.    if((r == g) && (g == b)) {
  2353.  
  2354.       rv = upd_truncate(upd,0,r);
  2355.  
  2356.    } else {
  2357.  
  2358.       gx_color_value w = g < r ? g : r; w = w < b ? w : b; /* Minimum */
  2359.  
  2360.       rv = upd_truncate(upd,0,w) | upd_truncate(upd,1,r) |
  2361.            upd_truncate(upd,2,g) | upd_truncate(upd,3,b);
  2362.  
  2363. /* It might still become a "gx_no_color_value" due to truncation, thus: */
  2364.  
  2365.       if(rv == gx_no_color_index) rv ^= 1;
  2366.    }
  2367.  
  2368. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2369.   fprintf(stderr,
  2370.    "rgb_4color: (%5.1f,%5.1f,%5.1f) : (%5.1f,%5.1f,%5.1f,%5.1f) : 0x%0*lx\n",
  2371.    255.0 * (double) r / (double) gx_max_color_value,
  2372.    255.0 * (double) g / (double) gx_max_color_value,
  2373.    255.0 * (double) b / (double) gx_max_color_value,
  2374.    255.0 * (double) ((rv >> upd->cmap[1].bitshf) & upd->cmap[1].bitmsk)
  2375.                  / (double) upd->cmap[1].bitmsk,
  2376.    255.0 * (double) ((rv >> upd->cmap[2].bitshf) & upd->cmap[2].bitmsk)
  2377.                  / (double) upd->cmap[2].bitmsk,
  2378.    255.0 * (double) ((rv >> upd->cmap[3].bitshf) & upd->cmap[3].bitmsk)
  2379.                  / (double) upd->cmap[3].bitmsk,
  2380.    255.0 * (double) ((rv >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2381.                  / (double) upd->cmap[0].bitmsk,
  2382.    (pdev->color_info.depth + 3)>>2,rv);
  2383. #endif
  2384.  
  2385.    return rv;
  2386. }
  2387.  
  2388. /* ------------------------------------------------------------------- */
  2389. /* upd_4color_rgb: Stored WRGB-Index back to a RGB                     */
  2390. /* ------------------------------------------------------------------- */
  2391.  
  2392. private int
  2393. upd_4color_rgb(gx_device *pdev, gx_color_index color, gx_color_value prgb[3])
  2394. {
  2395.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2396.  
  2397. /*
  2398.  * Expand to the Component-Values
  2399.  */
  2400.    prgb[0] = upd_expand(upd,1,color);
  2401.    prgb[1] = upd_expand(upd,2,color);
  2402.    prgb[2] = upd_expand(upd,3,color);
  2403.  
  2404. /* Revert our Grayscale-Trick: */
  2405.    if(!(prgb[0] || prgb[1] || prgb[2]))
  2406.       prgb[0] = prgb[1] = prgb[2] = upd_expand(upd,0,color);
  2407.  
  2408.  
  2409. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2410.    fprintf(stderr,
  2411.     "4color_rgb: 0x%0*lx -> (%5.1f,%5.1f,%5.1f,%5.1f) -> (%5.1f,%5.1f,%5.1f)\n",
  2412.     (pdev->color_info.depth + 3)>>2,color,
  2413.     255.0 * (double) ((color >> upd->cmap[1].bitshf) & upd->cmap[1].bitmsk)
  2414.                      / (double) upd->cmap[1].bitmsk,
  2415.     255.0 * (double) ((color >> upd->cmap[2].bitshf) & upd->cmap[2].bitmsk)
  2416.                      / (double) upd->cmap[2].bitmsk,
  2417.     255.0 * (double) ((color >> upd->cmap[3].bitshf) & upd->cmap[3].bitmsk)
  2418.                      / (double) upd->cmap[3].bitmsk,
  2419.     255.0 * (double) ((color >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2420.                      / (double) upd->cmap[0].bitmsk,
  2421.     255.0 * (double) prgb[0] / (double) gx_max_color_value,
  2422.     255.0 * (double) prgb[1] / (double) gx_max_color_value,
  2423.     255.0 * (double) prgb[2] / (double) gx_max_color_value);
  2424. #endif
  2425.  
  2426.    return 0;
  2427. }
  2428.  
  2429. /* ------------------------------------------------------------------- */
  2430. /* upd_cmyk_kcolor: KCMY->KCMY-Index Mapping with Black Generation     */
  2431. /* ------------------------------------------------------------------- */
  2432.  
  2433. private gx_color_index
  2434. upd_cmyk_kcolor(gx_device *pdev,
  2435.    gx_color_value c, gx_color_value m, gx_color_value y,gx_color_value k)
  2436. {
  2437.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2438.    gx_color_index  rv;
  2439.    gx_color_value  black;
  2440.  
  2441.    if((c == m) && (m == y)) {
  2442.  
  2443.       black = c > k ? c : k;
  2444.       rv = upd_truncate(upd,0,black);
  2445.  
  2446.    } else {
  2447.  
  2448.       if(k && !(c | m | y)) {
  2449.          black = k;
  2450.       } else {
  2451.          black = c     < m ? c     : m;
  2452.          black = black < y ? black : y;
  2453.       }
  2454.  
  2455.       rv  = upd_truncate(upd,0,black) | upd_truncate(upd,1,c)
  2456.           | upd_truncate(upd,2,m)     | upd_truncate(upd,3,y);
  2457.  
  2458. /* It might still become a "gx_no_color_value" due to truncation, thus: */
  2459.  
  2460.       if(rv == gx_no_color_index) rv ^= 1;
  2461.    }
  2462.  
  2463.  
  2464. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2465.   fprintf(stderr,
  2466. "cmyk_kcolor: (%5.1f,%5.1f,%5.1f,%5.1f) : (%5.1f,%5.1f,%5.1f,%5.1f) : 0x%0*lx\n",
  2467.    255.0 * (double) c / (double) gx_max_color_value,
  2468.    255.0 * (double) m / (double) gx_max_color_value,
  2469.    255.0 * (double) y / (double) gx_max_color_value,
  2470.    255.0 * (double) k / (double) gx_max_color_value,
  2471.    255.0 * (double) ((rv >> upd->cmap[1].bitshf) & upd->cmap[1].bitmsk)
  2472.                  / (double) upd->cmap[1].bitmsk,
  2473.    255.0 * (double) ((rv >> upd->cmap[2].bitshf) & upd->cmap[2].bitmsk)
  2474.                  / (double) upd->cmap[2].bitmsk,
  2475.    255.0 * (double) ((rv >> upd->cmap[3].bitshf) & upd->cmap[3].bitmsk)
  2476.                  / (double) upd->cmap[3].bitmsk,
  2477.    255.0 * (double) ((rv >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2478.                  / (double) upd->cmap[0].bitmsk,
  2479.    (pdev->color_info.depth + 3)>>2,rv);
  2480. #endif
  2481.  
  2482.    return rv;
  2483. }
  2484.  
  2485. /* ------------------------------------------------------------------- */
  2486. /* upd_kcolor_rgb: Stored CMY+generated K back to a RGB                */
  2487. /* ------------------------------------------------------------------- */
  2488.  
  2489. private int
  2490. upd_kcolor_rgb(gx_device *pdev, gx_color_index color, gx_color_value prgb[3])
  2491. {
  2492.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2493.    gx_color_value c,m,y,k;
  2494.  
  2495. /*
  2496.  * Expand to the Component-Values
  2497.  */
  2498.    k = upd_expand(upd,0,color);
  2499.    c = upd_expand(upd,1,color);
  2500.    m = upd_expand(upd,2,color);
  2501.    y = upd_expand(upd,3,color);
  2502.  
  2503. /*
  2504.  * Check for plain Gray-Values
  2505.  */
  2506.    if(!(c | m | y )) {
  2507.  
  2508.       prgb[2] = prgb[1] = prgb[0] = gx_max_color_value - k;
  2509.  
  2510.    } else {
  2511.       prgb[0] = gx_max_color_value - c;
  2512.       prgb[1] = gx_max_color_value - m;
  2513.       prgb[2] = gx_max_color_value - y;
  2514.    }
  2515.  
  2516. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2517.    fprintf(stderr,
  2518.     "kcolor_rgb: 0x%0*lx -> (%5.1f,%5.1f,%5.1f,%5.1f) -> (%5.1f,%5.1f,%5.1f,%5.1f) -> (%5.1f,%5.1f,%5.1f)\n",
  2519.     (pdev->color_info.depth + 3)>>2,color,
  2520.     255.0 * (double) ((color >> upd->cmap[1].bitshf) & upd->cmap[1].bitmsk)
  2521.                      / (double) upd->cmap[1].bitmsk,
  2522.     255.0 * (double) ((color >> upd->cmap[2].bitshf) & upd->cmap[2].bitmsk)
  2523.                      / (double) upd->cmap[2].bitmsk,
  2524.     255.0 * (double) ((color >> upd->cmap[3].bitshf) & upd->cmap[3].bitmsk)
  2525.                      / (double) upd->cmap[3].bitmsk,
  2526.     255.0 * (double) ((color >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2527.                      / (double) upd->cmap[0].bitmsk,
  2528.     255.0 * (double)   c     / (double) gx_max_color_value,
  2529.     255.0 * (double)   m     / (double) gx_max_color_value,
  2530.     255.0 * (double)   y     / (double) gx_max_color_value,
  2531.     255.0 * (double)   k     / (double) gx_max_color_value,
  2532.     255.0 * (double) prgb[0] / (double) gx_max_color_value,
  2533.     255.0 * (double) prgb[1] / (double) gx_max_color_value,
  2534.     255.0 * (double) prgb[2] / (double) gx_max_color_value);
  2535. #endif
  2536.  
  2537.    return 0;
  2538. }
  2539.  
  2540. /* ------------------------------------------------------------------- */
  2541. /* upd_rgb_ovcolor: Create an KCMY-Index from RGB                      */
  2542. /* ------------------------------------------------------------------- */
  2543.  
  2544. private gx_color_index
  2545. upd_rgb_ovcolor(gx_device *pdev,
  2546.    gx_color_value r, gx_color_value g, gx_color_value b)
  2547. {
  2548.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2549.    gx_color_index  rv;
  2550.    gx_color_value  c,m,y,black;
  2551.  
  2552.    if((r == g) && (g == b)) {
  2553.  
  2554.       black  = gx_max_color_value - r;
  2555.       rv     = upd_truncate(upd,0,black);
  2556.       c = m = y = 0;
  2557.  
  2558.    } else {
  2559.  
  2560.       c = gx_max_color_value - r;
  2561.       m = gx_max_color_value - g;
  2562.       y = gx_max_color_value - b;
  2563.  
  2564.       black = c     < m ? c     : m; 
  2565.       black = black < y ? black : y;
  2566.  
  2567.       if(black != gx_max_color_value) {
  2568.         float tmp,d;
  2569.         
  2570.         d   = gx_max_color_value - black;
  2571.  
  2572.         tmp = (float) (c-black) / d;
  2573.         if(      0.0 > tmp) tmp = 0.0;
  2574.         else if( 1.0 < tmp) tmp = 1.0;
  2575.         c   = tmp * gx_max_color_value + 0.499;
  2576.  
  2577.         tmp = (float) (m-black) / d;
  2578.         if(      0.0 > tmp) tmp = 0.0;
  2579.         else if( 1.0 < tmp) tmp = 1.0;
  2580.         m   = tmp * gx_max_color_value + 0.499;
  2581.  
  2582.         tmp = (float) (y-black) / d;
  2583.         if(      0.0 > tmp) tmp = 0.0;
  2584.         else if( 1.0 < tmp) tmp = 1.0;
  2585.         y   = tmp * gx_max_color_value + 0.499;
  2586.  
  2587.       } else {
  2588.  
  2589.         c = m = y = gx_max_color_value;
  2590.  
  2591.       }
  2592.  
  2593.       rv = upd_truncate(upd,0,black) | upd_truncate(upd,1,c) |
  2594.            upd_truncate(upd,2,m)     | upd_truncate(upd,3,y);
  2595.  
  2596. /* It might still become a "gx_no_color_value" due to truncation, thus: */
  2597.  
  2598.       if(rv == gx_no_color_index) rv ^= 1;
  2599.    }
  2600.  
  2601. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2602.   fprintf(stderr,
  2603.    "rgb_ovcolor: (%5.1f,%5.1f,%5.1f) : (%5.1f,%5.1f,%5.1f,%5.1f) : 0x%0*lx\n",
  2604.    255.0 * (double) r / (double) gx_max_color_value,
  2605.    255.0 * (double) g / (double) gx_max_color_value,
  2606.    255.0 * (double) b / (double) gx_max_color_value,
  2607.    255.0 * (double) ((rv >> upd->cmap[1].bitshf) & upd->cmap[1].bitmsk)
  2608.                  / (double) upd->cmap[1].bitmsk,
  2609.    255.0 * (double) ((rv >> upd->cmap[2].bitshf) & upd->cmap[2].bitmsk)
  2610.                  / (double) upd->cmap[2].bitmsk,
  2611.    255.0 * (double) ((rv >> upd->cmap[3].bitshf) & upd->cmap[3].bitmsk)
  2612.                  / (double) upd->cmap[3].bitmsk,
  2613.    255.0 * (double) ((rv >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2614.                  / (double) upd->cmap[0].bitmsk,
  2615.    (pdev->color_info.depth + 3)>>2,rv);
  2616. #endif
  2617.  
  2618.    return rv;
  2619. }
  2620.  
  2621. /* ------------------------------------------------------------------- */
  2622. /* upd_rgb_novcolor: Create an KCMY-Index from RGB                      */
  2623. /* ------------------------------------------------------------------- */
  2624.  
  2625. private gx_color_index
  2626. upd_rgb_novcolor(gx_device *pdev,
  2627.    gx_color_value r, gx_color_value g, gx_color_value b)
  2628. {
  2629.    const upd_p     upd = ((upd_device *)pdev)->upd;
  2630.    gx_color_index  rv;
  2631.    gx_color_value  c,m,y,black;
  2632.  
  2633.    if((r == g) && (g == b)) {
  2634.  
  2635.       black  = gx_max_color_value - r;
  2636.       rv     = upd_truncate(upd,0,black);
  2637.       c = m = y = 0;
  2638.  
  2639.    } else {
  2640.  
  2641.       c = gx_max_color_value - r;
  2642.       m = gx_max_color_value - g;
  2643.       y = gx_max_color_value - b;
  2644.  
  2645.       black = c     < m ? c     : m; 
  2646.       black = black < y ? black : y;
  2647.       c     = c - black;
  2648.       m     = m - black;
  2649.       y     = y - black;
  2650.  
  2651.       rv = upd_truncate(upd,0,black) | upd_truncate(upd,1,c) |
  2652.            upd_truncate(upd,2,m)     | upd_truncate(upd,3,y);
  2653.  
  2654. /* It might still become a "gx_no_color_value" due to truncation, thus: */
  2655.  
  2656.       if(rv == gx_no_color_index) rv ^= 1;
  2657.    }
  2658.  
  2659. #if UPD_MESSAGES & UPD_M_MAPCALLS
  2660.   fprintf(stderr,
  2661.    "rgb_ovcolor: (%5.1f,%5.1f,%5.1f) : (%5.1f,%5.1f,%5.1f,%5.1f) : 0x%0*lx\n",
  2662.    255.0 * (double) r / (double) gx_max_color_value,
  2663.    255.0 * (double) g / (double) gx_max_color_value,
  2664.    255.0 * (double) b / (double) gx_max_color_value,
  2665.    255.0 * (double) ((rv >> upd->cmap[1].bitshf) & upd->cmap[1].bitmsk)
  2666.                  / (double) upd->cmap[1].bitmsk,
  2667.    255.0 * (double) ((rv >> upd->cmap[2].bitshf) & upd->cmap[2].bitmsk)
  2668.                  / (double) upd->cmap[2].bitmsk,
  2669.    255.0 * (double) ((rv >> upd->cmap[3].bitshf) & upd->cmap[3].bitmsk)
  2670.                  / (double) upd->cmap[3].bitmsk,
  2671.    255.0 * (double) ((rv >> upd->cmap[0].bitshf) & upd->cmap[0].bitmsk)
  2672.                  / (double) upd->cmap[0].bitmsk,
  2673.    (pdev->color_info.depth + 3)>>2,rv);
  2674. #endif
  2675.  
  2676.    return rv;
  2677. }
  2678.  
  2679. /* ------------------------------------------------------------------- */
  2680. /* NOTE: Beyond this point only "uniprint"-special-items.              */
  2681. /* ------------------------------------------------------------------- */
  2682.  
  2683. /* ------------------------------------------------------------------- */
  2684. /* Return the gx_color_value for a given component                     */
  2685. /* ------------------------------------------------------------------- */
  2686.  
  2687. private gx_color_value
  2688. upd_expand(upd_pc upd,int i,uint32 ci)
  2689. {
  2690.    const updcmap_pc cmap = upd->cmap + i;    /* Writing-Shortcut */
  2691.  
  2692.    ci = (ci >> cmap->bitshf) & cmap->bitmsk; /* Extract the component */
  2693.    if(!cmap->rise) ci = cmap->bitmsk - ci;   /* Invert, if necessary */
  2694. /* no Truncation/Expansion on full range */
  2695.    if(gx_color_value_bits > cmap->bits) return cmap->code[ci];
  2696.    else                                 return (gx_color_value) ci;
  2697. }
  2698. /* That's simple, isn't it? */
  2699.  
  2700. /* ------------------------------------------------------------------- */
  2701. /* Truncate a gx_color_value to the desired number of bits.            */
  2702. /* ------------------------------------------------------------------- */
  2703.  
  2704. private uint32
  2705. upd_truncate(upd_pc upd,int i,gx_color_value v) {
  2706.    const updcmap_pc cmap = upd->cmap + i;
  2707.    int32           s; /* step size */
  2708.    gx_color_value *p; /* value-pointer */
  2709.  
  2710.    if(0 == cmap->bits) {                          /* trivial case */
  2711.  
  2712.       v = 0;
  2713.  
  2714.    } else if(gx_color_value_bits > cmap->bits) { /* really truncate ? */
  2715.  
  2716.       p = cmap->code + ((cmap->bitmsk + 1) >> 1);
  2717.       s =              ((cmap->bitmsk + 1) >> 2);
  2718. /*
  2719.  * Perform search in monotonic code-array
  2720.  */
  2721.       while(s > 0) {
  2722.          if(v > *p) {           /* we're below */
  2723.             p += s;
  2724.          } else if(v < p[-1]) { /* we're ahead for sure */
  2725.             p -= s;
  2726.          } else {
  2727. /* years ago, i knew what this was good for */
  2728.             if((v-p[-1]) < (p[0]-v)) p -= 1;
  2729.             break;
  2730.          }
  2731.          s >>= 1;
  2732.       }
  2733.       if((v-p[-1]) < (p[0]-v)) p -= 1;
  2734.       v = p - cmap->code;
  2735.    }
  2736.  
  2737.    if(!cmap->rise) v = cmap->bitmsk - v; /* Again reverse, if necessary */
  2738.  
  2739.    return ((uint32) v) << cmap->bitshf;
  2740. }
  2741.  
  2742. /* ------------------------------------------------------------------- */
  2743. /* upd_open_map: install the color-mapping                             */
  2744. /* ------------------------------------------------------------------- */
  2745.  
  2746. private int
  2747. upd_open_map(upd_device *udev)
  2748. {
  2749.    const upd_p      upd   = udev->upd;
  2750.    int imap;
  2751.  
  2752. /** _always_ initialize crucial Values! */
  2753.    for(imap = 0; UPD_CMAP_MAX > imap; ++imap) upd->cmap[imap].code   = NULL;
  2754.    upd->ncomp = 0;
  2755.  
  2756. /** There should not be an error yet */
  2757.    if(B_ERROR & upd->flags)    imap = 0;
  2758.  
  2759. /** Establish the xfer-Indices */
  2760.    if(imap) {
  2761.       for(imap = 0; UPD_CMAP_MAX > imap; ++imap) {
  2762.          upd->cmap[imap].xfer = -1;
  2763.          upd->cmap[imap].bits =  0;
  2764.       }
  2765.       switch(upd->choice[C_MAPPER]) {
  2766.          case MAP_GRAY:
  2767.             upd->cmap[0].xfer = FA_WXFER;
  2768.          break;
  2769.          case MAP_RGBW:
  2770.             upd->cmap[0].xfer = FA_WXFER;
  2771.             upd->cmap[1].xfer = FA_RXFER;
  2772.             upd->cmap[2].xfer = FA_GXFER;
  2773.             upd->cmap[3].xfer = FA_BXFER;
  2774.          break;
  2775.          case MAP_RGB:
  2776.             upd->cmap[0].xfer = FA_RXFER;
  2777.             upd->cmap[1].xfer = FA_GXFER;
  2778.             upd->cmap[2].xfer = FA_BXFER;
  2779.          break;
  2780.          case MAP_CMYK:
  2781.             upd->cmap[0].xfer = FA_KXFER;
  2782.             upd->cmap[1].xfer = FA_CXFER;
  2783.             upd->cmap[2].xfer = FA_MXFER;
  2784.             upd->cmap[3].xfer = FA_YXFER;
  2785.          break;
  2786.          case MAP_CMYKGEN:
  2787.             upd->cmap[0].xfer = FA_KXFER;
  2788.             upd->cmap[1].xfer = FA_CXFER;
  2789.             upd->cmap[2].xfer = FA_MXFER;
  2790.             upd->cmap[3].xfer = FA_YXFER;
  2791.          break;
  2792.          case MAP_RGBOV:
  2793.             upd->cmap[0].xfer = FA_KXFER;
  2794.             upd->cmap[1].xfer = FA_CXFER;
  2795.             upd->cmap[2].xfer = FA_MXFER;
  2796.             upd->cmap[3].xfer = FA_YXFER;
  2797.          break;
  2798.          case MAP_RGBNOV:
  2799.             upd->cmap[0].xfer = FA_KXFER;
  2800.             upd->cmap[1].xfer = FA_CXFER;
  2801.             upd->cmap[2].xfer = FA_MXFER;
  2802.             upd->cmap[3].xfer = FA_YXFER;
  2803.          break;
  2804.          default:
  2805. #if         UPD_MESSAGES & UPD_M_WARNING
  2806.                if(upd_choice[C_MAPPER][0])
  2807.                   fprintf(stderr,
  2808.                      "upd_open_map: unsupported %s=%d\n",
  2809.                      upd_choice[C_MAPPER][0],upd->choice[C_MAPPER]);
  2810.                else
  2811.                   fprintf(stderr,
  2812.                      "upd_open_map: unsupported choce[%d]=%d\n",
  2813.                      C_MAPPER,upd->choice[C_MAPPER]);
  2814. #endif
  2815.             imap = 0;
  2816.          break;
  2817.       }
  2818.    }
  2819.  
  2820.  
  2821. /** The bit number sould be positive & fit into the storage */
  2822.  
  2823.    if(imap) { /* Check number of Bits & Shifts */
  2824.  
  2825. #if      UPD_MESSAGES & UPD_M_WARNING
  2826.       uint32 used = 0,bitmsk;
  2827. #endif
  2828.       bool success = true;
  2829.  
  2830.       for(imap = 0; UPD_CMAP_MAX > imap; ++imap) {
  2831.          if(0 > upd->cmap[imap].xfer) continue;
  2832.  
  2833.          if((0                     > upd->int_a[IA_COMPBITS].data[imap])  ||
  2834.             (gx_color_value_bits   < upd->int_a[IA_COMPBITS].data[imap])  ||
  2835.             (0                     > upd->int_a[IA_COMPSHIFT].data[imap]) ||
  2836.             (upd->int_a[IA_COMPBITS].data[imap] >
  2837.              (udev->color_info.depth - upd->int_a[IA_COMPSHIFT].data[imap]))) {
  2838. #if         UPD_MESSAGES & UPD_M_WARNING
  2839.                fprintf(stderr,
  2840.                   "upd_open_map: %d Bits << %d is illegal for %d. Component\n",
  2841.                   upd->int_a[IA_COMPBITS].data[imap],
  2842.                   upd->int_a[IA_COMPSHIFT].data[imap],imap+1);
  2843. #endif
  2844.  
  2845.             success = false;
  2846.  
  2847.  
  2848.          } else {
  2849.  
  2850.             int         n;
  2851.             const float *now;
  2852.             float       last;
  2853.  
  2854.             if((NULL == upd->float_a[upd->cmap[imap].xfer].data) ||
  2855.                (2    >  upd->float_a[upd->cmap[imap].xfer].size)   ) {
  2856.                float *fp;
  2857.                UPD_MM_DEL_PARAM(upd->float_a[upd->cmap[imap].xfer]);
  2858.                UPD_MM_GET_ARRAY(fp,2);
  2859.                fp[0] = 0.0;
  2860.                fp[1] = 1.0;
  2861.                upd->float_a[upd->cmap[imap].xfer].data = fp;
  2862.                upd->float_a[upd->cmap[imap].xfer].size = 2;
  2863.             }
  2864.             n    = upd->float_a[upd->cmap[imap].xfer].size-1;
  2865.             now  = upd->float_a[upd->cmap[imap].xfer].data;
  2866.             last = now[n];
  2867.  
  2868.             if(     *now < last) { /* Rising */
  2869.                last = *now++;
  2870.                while(n--) {
  2871.                  if(last >= *now) break;
  2872.                  last = *now++;
  2873.                }
  2874.             } else if(*now > last) { /* Falling */
  2875.                last = *now++;
  2876.                while(n--) {
  2877.                  if(last <= *now) break;
  2878.                  last = *now++;
  2879.                }
  2880.             }                      /* Monotony-check */
  2881.  
  2882.             if(0 <= n) {
  2883. #if            UPD_MESSAGES & UPD_M_WARNING
  2884.                fprintf(stderr,
  2885.                   "upd_open_map: %d. Component has non monoton Xfer\n",imap+1);
  2886. #endif
  2887.                success = false;
  2888.  
  2889.             } else {
  2890.  
  2891. #if            UPD_MESSAGES & UPD_M_WARNING
  2892.  
  2893.                bitmsk   = ((uint32) 1 << upd->int_a[IA_COMPBITS].data[imap]) -1;
  2894.                bitmsk <<= upd->int_a[IA_COMPSHIFT].data[imap];
  2895.  
  2896.                if(used & bitmsk) fprintf(stderr,
  2897.                   "upd_open_map: %d. Component overlaps with others\n",imap+1);
  2898.  
  2899.                used |= bitmsk;
  2900. #endif
  2901.             }
  2902.          }
  2903.       }
  2904.  
  2905.       if(!success) imap = 0;
  2906.  
  2907.    }             /* Check number of Bits */
  2908.  
  2909. /** Do the allocation */
  2910.  
  2911.    if(imap) {
  2912.  
  2913.       for(imap = 0; UPD_CMAP_MAX > imap; ++imap) {
  2914.          if(0 > upd->cmap[imap].xfer) continue;
  2915.  
  2916.          upd->cmap[imap].bits     = upd->int_a[IA_COMPBITS].data[imap];
  2917.          upd->cmap[imap].bitshf   = upd->int_a[IA_COMPSHIFT].data[imap];
  2918.          upd->cmap[imap].bitmsk   = 1;
  2919.          upd->cmap[imap].bitmsk <<= upd->cmap[imap].bits;
  2920.          upd->cmap[imap].bitmsk  -= 1;
  2921.          upd->cmap[imap].rise     =
  2922.             upd->float_a[upd->cmap[imap].xfer].data[0] <
  2923.             upd->float_a[upd->cmap[imap].xfer].data[
  2924.                upd->float_a[upd->cmap[imap].xfer].size-1] ?
  2925.             true : false;
  2926.          upd->cmap[imap].code     = gs_malloc(upd->cmap[imap].bitmsk+1,
  2927.              sizeof(upd->cmap[imap].code[0]),"upd/code");
  2928.          if(!upd->cmap[imap].code) break;
  2929.       }
  2930.  
  2931.       if(UPD_CMAP_MAX > imap) {
  2932.  
  2933.          imap = 0;
  2934.  
  2935. #if      UPD_MESSAGES & UPD_M_ERROR
  2936.             fprintf(stderr,"upd_open_map: could not allocate code-arrays\n");
  2937. #        endif
  2938.  
  2939.       }
  2940.    }
  2941.  
  2942. /** then fill the code-arrays */
  2943.  
  2944.    if(imap) {
  2945. /*
  2946.  * Try making things easier: (than with stcolor)
  2947.  *     normalize values to 0.0/1.0-Range
  2948.  *     X-Axis:   Color-Values (implied)
  2949.  *     Y-Values: Indices      (given)
  2950.  */
  2951.  
  2952.       for(imap = 0; UPD_CMAP_MAX > imap; ++imap) {
  2953.  
  2954.          const updcmap_p cmap = upd->cmap + imap;
  2955.          uint32 ly,iy;
  2956.          float ystep,xstep,fx,fy;
  2957.  
  2958. /*       Variables & Macro for Range-Normalization */
  2959.          double offset,scale;
  2960. #define  XFVAL(I) ((upd->float_a[cmap->xfer].data[I]-offset)*scale)
  2961.  
  2962.          if(0 > cmap->xfer) continue;
  2963.  
  2964.          cmap->code[cmap->bitmsk] = gx_max_color_value;
  2965.  
  2966.          if(!cmap->bits) continue;
  2967.  
  2968.          offset = upd->float_a[cmap->xfer].data[0];
  2969.          if(     0.0 > offset) offset = 0.0;
  2970.          else if(1.0 < offset) offset = 1.0;
  2971.  
  2972.          scale  = upd->float_a[cmap->xfer].data[upd->float_a[cmap->xfer].size-1];
  2973.          if(     0.0 > scale ) scale  = 0.0;
  2974.          else if(1.0 < scale ) scale  = 1.0;
  2975.  
  2976.          if(scale != offset) scale = 1.0 / (scale - offset);
  2977.          else                scale = 0.0;
  2978.  
  2979. /*       interpolate */
  2980.          ystep = (float) 1.0 / (float) cmap->bitmsk;
  2981.          xstep = (float) 1.0 / (float)(upd->float_a[cmap->xfer].size - 1);
  2982.  
  2983.          iy = 0;
  2984.          for(ly = 0; ly <= cmap->bitmsk; ++ly) {
  2985.  
  2986.             fy = ystep * ly; /* Target-Value */
  2987.  
  2988.             while(((iy+2) < upd->float_a[cmap->xfer].size) &&
  2989.                   (fy > XFVAL(iy+1))) ++iy;
  2990.  
  2991.             fx  = iy + (fy - XFVAL(iy))/(XFVAL(iy+1) - XFVAL(iy));
  2992.  
  2993.             fx *= xstep * gx_max_color_value;
  2994.  
  2995.             fx  = fx < 0.0 ? 0.0 :
  2996.                  (fx > gx_max_color_value ? gx_max_color_value : fx);
  2997.  
  2998.             cmap->code[ly] = fx;
  2999.             if((fx - cmap->code[ly]) >= 0.5) cmap->code[ly] += 1;
  3000.          }
  3001.  
  3002. #undef   XFVAL
  3003.  
  3004.       }
  3005.    }
  3006.  
  3007. /** If we're ok, massage upd->ncomp */
  3008.  
  3009.    if(imap) {
  3010.       switch(upd->choice[C_MAPPER]) {
  3011.          case MAP_GRAY:
  3012.            if(1 > imap) imap = 0;
  3013.            upd->ncomp = 1;
  3014.          break;
  3015.          case MAP_RGBW: /* RGB->RGBW */
  3016.            if(4 > imap) imap = 0;
  3017.            upd->ncomp = 4;
  3018.          break;
  3019.          case MAP_RGB: /* Plain RGB */
  3020.            if(3 > imap) imap = 0;
  3021.            upd->ncomp = 3;
  3022.          break;
  3023.          case MAP_CMYK: /* Plain KCMY */
  3024.            if(4 > imap) imap = 0;
  3025.             upd->ncomp = 4;
  3026.          break;
  3027.          case MAP_CMYKGEN: /* KCMY with black-generation */
  3028.            if(4 > imap) imap = 0;
  3029.            upd->ncomp = 4;
  3030.          break;
  3031.          case MAP_RGBOV: /* RGB->KCMY with black-generation */
  3032.            if(4 > imap) imap = 0;
  3033.            upd->ncomp = 4;
  3034.          break;
  3035.          case MAP_RGBNOV: /* RGB->KCMY with black-generation */
  3036.            if(4 > imap) imap = 0;
  3037.            upd->ncomp = 4;
  3038.          break;
  3039.  
  3040.          default:
  3041.            imap = 0;
  3042. #if        UPD_MESSAGES & UPD_M_WARNING
  3043.               fprintf(stderr,
  3044.                  "upd_open: Mapping %d unknown\n",upd->choice[C_MAPPER]);
  3045. #endif
  3046.  
  3047.          break;
  3048.       }
  3049.    }
  3050.  
  3051.  
  3052. /** If unsuccesful, install the default routines */
  3053.  
  3054.    if(!imap) {
  3055.       upd_close_map(udev);
  3056.    } else {
  3057.       upd->flags |= B_MAP;
  3058.       upd_procs_map(udev);
  3059.    }
  3060.  
  3061.    return imap ? 1 : -1;
  3062. }
  3063.  
  3064. /* ------------------------------------------------------------------- */
  3065. /* upd_procs_map: (de-) install the color-mapping-procedures           */
  3066. /* ------------------------------------------------------------------- */
  3067.  
  3068. private int
  3069. upd_procs_map(upd_device *udev)
  3070. {
  3071.    int imap;
  3072.  
  3073.    if( udev->upd &&
  3074.       (udev->upd->flags & B_MAP)) imap = udev->upd->choice[C_MAPPER];
  3075.    else                           imap = 0;
  3076.  
  3077.    switch(imap) {
  3078.      case MAP_GRAY: /* Grayscale -> Grayscale */
  3079.        set_dev_proc(udev,map_rgb_color, upd_rgb_1color);
  3080.        set_dev_proc(udev,map_cmyk_color,gx_default_map_cmyk_color);
  3081.        set_dev_proc(udev,map_color_rgb, upd_1color_rgb);
  3082.      break;
  3083.      case MAP_RGBW: /* RGB->RGBW */
  3084.        set_dev_proc(udev,map_rgb_color, upd_rgb_4color);
  3085.        set_dev_proc(udev,map_cmyk_color,gx_default_map_cmyk_color);
  3086.        set_dev_proc(udev,map_color_rgb, upd_4color_rgb);
  3087.      break;
  3088.      case MAP_RGB: /* Plain RGB */
  3089.        set_dev_proc(udev,map_rgb_color, upd_rgb_3color);
  3090.        set_dev_proc(udev,map_cmyk_color,gx_default_map_cmyk_color);
  3091.        set_dev_proc(udev,map_color_rgb, upd_3color_rgb);
  3092.      break;
  3093.      case MAP_CMYK: /* Plain KCMY */
  3094.        set_dev_proc(udev,map_rgb_color, gx_default_map_rgb_color);
  3095.        set_dev_proc(udev,map_cmyk_color,upd_cmyk_icolor);
  3096.        set_dev_proc(udev,map_color_rgb, upd_icolor_rgb);
  3097.      break;
  3098.      case MAP_CMYKGEN: /* KCMY with black-generation */
  3099.        set_dev_proc(udev,map_rgb_color, gx_default_map_rgb_color);
  3100.        set_dev_proc(udev,map_cmyk_color,upd_cmyk_kcolor);
  3101.        set_dev_proc(udev,map_color_rgb, upd_kcolor_rgb);
  3102.      break;
  3103.      case MAP_RGBOV: /* RGB -> KCMY with BG and UCR for CMYK-Output */
  3104.        set_dev_proc(udev,map_rgb_color, upd_rgb_ovcolor);
  3105.        set_dev_proc(udev,map_cmyk_color,gx_default_map_cmyk_color);
  3106.        set_dev_proc(udev,map_color_rgb, upd_ovcolor_rgb);
  3107.      break;
  3108.      case MAP_RGBNOV: /* RGB -> KCMY with BG and UCR for CMY+K-Output */
  3109.        set_dev_proc(udev,map_rgb_color, upd_rgb_novcolor);
  3110.        set_dev_proc(udev,map_cmyk_color,gx_default_map_cmyk_color);
  3111.        set_dev_proc(udev,map_color_rgb, upd_novcolor_rgb);
  3112.      break;
  3113.  
  3114.      default:
  3115.        set_dev_proc(udev,map_rgb_color, gx_default_map_rgb_color);
  3116.        set_dev_proc(udev,map_cmyk_color,gx_default_map_cmyk_color);
  3117.        set_dev_proc(udev,map_color_rgb, gx_default_map_color_rgb);
  3118.      break;
  3119.   }
  3120.   return 0;
  3121.  
  3122. }
  3123.  
  3124. /* ------------------------------------------------------------------- */
  3125. /* upd_close_map: remove color mapping                                 */
  3126. /* ------------------------------------------------------------------- */
  3127.  
  3128. private int
  3129. upd_close_map(upd_device *udev)
  3130. {
  3131.    const upd_p      upd   = udev->upd;
  3132.    int imap;
  3133.  
  3134.    if(upd) {
  3135.  
  3136.       for(imap = 0; UPD_CMAP_MAX > imap; ++imap) {
  3137.  
  3138.          if(upd->cmap[imap].code)
  3139.             gs_free(upd->cmap[imap].code,sizeof(upd->cmap[imap].code[0]),
  3140.                 upd->cmap[imap].bitmsk+1,"upd/code");
  3141.          upd->cmap[imap].code   = NULL;
  3142.  
  3143.          upd->cmap[imap].bitmsk = 0;
  3144.          upd->cmap[imap].bitshf = 0;
  3145.          upd->cmap[imap].bits   = 0;
  3146.          upd->cmap[imap].rise   = false;
  3147.       }
  3148.       upd->flags &= ~B_MAP;
  3149.    }
  3150.  
  3151.    upd_procs_map(udev);
  3152.  
  3153.    return 0;
  3154. }
  3155.  
  3156. /* ------------------------------------------------------------------- */
  3157. /* Functions for the rendering of data                                 */
  3158. /* ------------------------------------------------------------------- */
  3159.  
  3160. /**
  3161. Inside the main-upd-type are a "valbuf" and some unidentified
  3162. pointers. This stuff is used in conjunction with the rendering,
  3163. which is the process of converting gx_color_indices into something
  3164. suitable for the device.
  3165.  
  3166. */
  3167.  
  3168. /* ------------------------------------------------------------------- */
  3169. /* upd_open_render: Initialize rendering                               */
  3170. /* ------------------------------------------------------------------- */
  3171.  
  3172. private void
  3173. upd_open_render(upd_device *udev)
  3174. {
  3175.    const upd_p upd = udev->upd;
  3176.    int  icomp;
  3177.  
  3178. /** Reset everything related to rendering */
  3179.    upd->flags       &= ~B_RENDER;
  3180.    upd->valbuf       = NULL;
  3181.    upd->nvalbuf      = 0;
  3182.    upd->render       = NULL;
  3183.    upd->start_render = NULL;
  3184.    for(icomp = 0; UPD_VALPTR_MAX > icomp; ++icomp) upd->valptr[icomp] = NULL;
  3185.  
  3186.    if( (B_BUF | B_MAP) ==
  3187.       ((B_BUF | B_MAP | B_ERROR) & upd->flags)) {
  3188.  
  3189. /** Establish the renderingwidth in upd */
  3190.       upd->rwidth = upd->gswidth;
  3191.       if((0            < upd->ints[I_PWIDTH]) &&
  3192.          (upd->gswidth > upd->ints[I_PWIDTH])   )
  3193.           upd->rwidth  = upd->ints[I_PWIDTH];
  3194.  
  3195. /** Call the Render-specific Open-Function */
  3196.       switch(upd->choice[C_RENDER]) {
  3197.          case RND_FSCOMP:
  3198.             upd_open_fscomp(udev);
  3199.          break;
  3200.          case RND_FSCMYK:
  3201.             upd_open_fscmyk(udev);
  3202.          break;
  3203.          case RND_FSCMY_K:
  3204.             upd_open_fscmy_k(udev);
  3205.          break;
  3206.          default:
  3207. #if UPD_MESSAGES & UPD_M_WARNING
  3208.             fprintf(stderr,"upd_open_render: Unknown rendering type %d\n",
  3209.                 upd->choice[C_RENDER]);
  3210. #endif
  3211.          break;
  3212.       }
  3213.    }
  3214.  
  3215.    if(B_RENDER != ((B_ERROR | B_RENDER) & upd->flags))
  3216.       upd_close_render(udev);
  3217.  
  3218.    return;
  3219. }
  3220.  
  3221.  
  3222. /* ------------------------------------------------------------------- */
  3223. /* upd_close_render: Deinitialize rendering                            */
  3224. /* ------------------------------------------------------------------- */
  3225.  
  3226. private void
  3227. upd_close_render(upd_device *udev)
  3228. {
  3229.    const upd_p upd = udev->upd;
  3230.  
  3231.    if(upd) {
  3232.       int icomp;
  3233.  
  3234.       if((upd->render == upd_fscomp) ||
  3235.          (upd->render == upd_fscmyk)   )  upd_close_fscomp(udev);
  3236.  
  3237.       if((0 < upd->nvalbuf) && upd->valbuf)
  3238.          gs_free(upd->valbuf,upd->nvalbuf,sizeof(upd->valbuf[0]),"upd/valbuf");
  3239.       upd->valbuf  = NULL;
  3240.       upd->nvalbuf = 0;
  3241.  
  3242.       upd->flags       &= ~B_RENDER;
  3243.       upd->render       = NULL;
  3244.       upd->start_render = NULL;
  3245.       for(icomp = 0; UPD_VALPTR_MAX > icomp; ++icomp) upd->valptr[icomp] = NULL;
  3246.  
  3247.    }
  3248.    return;
  3249. }
  3250.  
  3251. /* ------------------------------------------------------------------- */
  3252. /* upd_open_fscomp: Initialize Component-Floyd-Steinberg               */
  3253. /* ------------------------------------------------------------------- */
  3254. #if UPD_MESSAGES & UPD_M_FSBUF
  3255. static int32 fs_emin[UPD_VALPTR_MAX],fs_emax[UPD_VALPTR_MAX];
  3256. #endif
  3257. private void
  3258. upd_open_fscomp(upd_device *udev)
  3259. {
  3260.    const upd_p upd = udev->upd;
  3261.    int icomp,order[UPD_CMAP_MAX];
  3262.  
  3263. #if UPD_MESSAGES & UPD_M_FSBUF
  3264.    for(icomp = 0; UPD_VALPTR_MAX < icomp; ++icomp)
  3265.       fs_emin[icomp] = fs_emax[icomp] = 0;
  3266. #endif
  3267.  
  3268.    icomp = upd->ncomp;
  3269.  
  3270.    if((0              >= icomp) ||
  3271.       (UPD_VALPTR_MAX <  icomp) ||
  3272.       (UPD_CMAP_MAX   <  icomp)   ) icomp      = 0;
  3273.  
  3274. /**
  3275. This Version of the FS-algorithm works on the mapped components, but
  3276. the printing-order might be different from the order dictated by the
  3277. mapping-routines. The optional COMPORDER-Array is used for that. The
  3278. initial test checks it's integrity.
  3279. */
  3280.    if(icomp) {
  3281.       if(upd->ncomp <= upd->int_a[IA_COMPORDER].size) { /* Reordering */
  3282.          bool success = true;
  3283.          for(icomp = 0; upd->ncomp > icomp; ++icomp) {
  3284.             order[icomp] = upd->int_a[IA_COMPORDER].data[icomp];
  3285.             if((0            >  order[icomp]) ||
  3286.                (UPD_CMAP_MAX <= order[icomp])   ) {
  3287.                success = false;
  3288. #if UPD_MESSAGES & UPD_M_WARNING
  3289.                   fprintf(stderr,
  3290.                    "upd_open_fscomp: %d is illegal component-index\n",
  3291.                    order[icomp]);
  3292. #endif
  3293.             }
  3294.          }
  3295.          if(!success) icomp = 0;
  3296.       } else {                                          /* Default-Ordering */
  3297.          for(icomp = 0; UPD_CMAP_MAX > icomp; ++icomp) order[icomp] = icomp;
  3298.       }                                                 /* Ordering defined */
  3299.    }
  3300.  
  3301. /**
  3302. If anything was ok. up to now, memory get's allocated.
  3303. */
  3304.    if(icomp) {
  3305.  
  3306.       for(icomp = 0; upd->ncomp > icomp; ++icomp) {
  3307.          upd->valptr[icomp] = gs_malloc(1,sizeof(updcomp_t),"upd/fscomp");
  3308.          if(NULL == upd->valptr[icomp]) {
  3309. #if UPD_MESSAGES & UPD_M_ERROR
  3310.             fprintf(stderr,
  3311.                "upd_open_fscomp: could not allocate %d. updcomp\n",
  3312.                icomp);
  3313. #endif
  3314.             icomp = 0;
  3315.             break;
  3316.          }
  3317.       }
  3318.    }
  3319.  
  3320.    if(icomp) {
  3321.       uint need;
  3322.  
  3323.       need  = (2 + upd->rwidth) * upd->ncomp;
  3324.       upd->valbuf = gs_malloc(need,sizeof(upd->valbuf[0]),"upd/valbuf");
  3325.  
  3326.       if(upd->valbuf) {
  3327.          upd->nvalbuf = need;
  3328.          memset(upd->valbuf,0,need*sizeof(upd->valbuf[0]));
  3329.       } else {
  3330. #if UPD_MESSAGES & UPD_M_ERROR
  3331.          fprintf(stderr,
  3332.             "upd_open_fscomp: could not allocate %u words for valbuf\n",need);
  3333. #endif
  3334.          icomp = 0;
  3335.       }
  3336.    }
  3337.  
  3338. /* Still happy? then compute component-values */
  3339.  
  3340.    if(icomp) {
  3341.       for(icomp = 0; upd->ncomp > icomp; ++icomp) {
  3342.  
  3343.          const updcomp_p comp   = upd->valptr[icomp];
  3344.          const int32     nsteps = upd->cmap[order[icomp]].bitmsk;
  3345.          float ymin,ymax;
  3346.          int32 highmod,highval;
  3347.          int i;
  3348.  
  3349.          comp->threshold = nsteps;
  3350.          comp->spotsize  = nsteps;
  3351.          comp->offset    = 0;
  3352.          comp->scale     = 1;
  3353.          comp->cmap      = order[icomp];
  3354.          upd->cmap[comp->cmap].comp = icomp;
  3355.          comp->bits      = upd->cmap[comp->cmap].bits;
  3356.          comp->bitshf    = upd->cmap[comp->cmap].bitshf;
  3357.          comp->bitmsk    = upd->cmap[comp->cmap].bitmsk;
  3358.  
  3359.          if(!nsteps) continue; /* A 0-Bit component is legal! */
  3360.  
  3361.          if(upd->cmap[comp->cmap].rise) {
  3362.             ymin = upd->float_a[upd->cmap[comp->cmap].xfer].data[0];
  3363.             ymax = upd->float_a[upd->cmap[comp->cmap].xfer].data[
  3364.                       upd->float_a[upd->cmap[comp->cmap].xfer].size-1];
  3365.          } else {
  3366.             ymax = upd->float_a[upd->cmap[comp->cmap].xfer].data[0];
  3367.             ymin = upd->float_a[upd->cmap[comp->cmap].xfer].data[
  3368.                       upd->float_a[upd->cmap[comp->cmap].xfer].size-1];
  3369.          }
  3370.  
  3371.          if(0.0 > ymin) {
  3372.             ymin = 0.0;
  3373.             if(0.0 > ymax) ymax = 1.0 / (float) (nsteps+1);
  3374.          }
  3375.          if(1.0 < ymax) ymax = 1.0;
  3376.  
  3377.          comp->spotsize = ((int32) 1 << 28) - 1;
  3378.  
  3379.          for(i = 0; i < 32; ++i) { /* Attempt Ideal */
  3380.  
  3381.             highval = (ymax-ymin) * (double) comp->spotsize + 0.5;
  3382.  
  3383.             if(!(highmod = highval % nsteps)) break; /* Gotcha */
  3384.  
  3385.             highval += nsteps - highmod;
  3386.             comp->spotsize = (double) highval / (ymax-ymin) + 0.5;
  3387.  
  3388.             if(!(comp->spotsize % 2)) comp->spotsize++;
  3389.  
  3390.          }                         /* Attempt Ideal */
  3391.  
  3392.          comp->offset    = ymin * (double) comp->spotsize + (double) 0.5;
  3393.          comp->scale     = highval / nsteps;
  3394.          comp->threshold = comp->spotsize / 2;
  3395.  
  3396. #if UPD_MESSAGES & UPD_M_SETUP
  3397.          fprintf(stderr,
  3398.              "Values for %d. Component after %d iterations\n",comp->cmap+1,i);
  3399.          fprintf(stderr,
  3400.              "steps:     %10ld, Bits: %d\n",(long) comp->bitmsk,comp->bits);
  3401.          fprintf(stderr,
  3402.              "xfer:      %10d Points, %s\n",
  3403.              upd->float_a[upd->cmap[comp->cmap].xfer].size,
  3404.              upd->cmap[comp->cmap].rise ? "rising" : "falling");
  3405.          fprintf(stderr,
  3406.              "offset:    %10d 0x%08x\n",comp->offset,comp->offset);
  3407.          fprintf(stderr,
  3408.              "scale:     %10d 0x%08x\n",comp->scale,comp->scale);
  3409.          fprintf(stderr,
  3410.              "threshold: %10d 0x%08x\n",comp->threshold,comp->threshold);
  3411.          fprintf(stderr,
  3412.              "spotsize:  %10d 0x%08x\n",comp->spotsize,comp->spotsize);
  3413. #endif
  3414.       }
  3415.    }
  3416. /**
  3417. Optional Random Initialization of the value-Buffer
  3418. */
  3419.    if(icomp && !(B_FSZERO & upd->flags)) {
  3420.       for(icomp = 0; icomp < upd->ncomp; ++icomp) {
  3421.          const updcomp_p comp = upd->valptr[icomp];
  3422.          int i;
  3423.          int32 lv = INT32_MAX, hv = INT32_MIN, v;
  3424.          float scale;
  3425.          for(i = icomp; i < upd->nvalbuf; i += upd->ncomp) {
  3426.             v = rand();
  3427.             if(lv > v) lv = v;
  3428.             if(hv < v) hv = v;
  3429.             upd->valbuf[i] = v;
  3430.          }
  3431.          scale = (float) comp->threshold / (float) (hv - lv);
  3432.          lv   += comp->threshold / (2*scale);
  3433.          for(i = icomp; i < upd->nvalbuf; i += upd->ncomp)
  3434.             upd->valbuf[i] = scale * (upd->valbuf[i] - lv);
  3435.       }
  3436.    }
  3437.  
  3438. /**
  3439. The render-Routine acts as an indicator, which render-close is to use!
  3440. */
  3441.    upd->render = upd_fscomp;
  3442.  
  3443.    if(icomp) upd->flags |=  B_RENDER;
  3444.    else      upd->flags &= ~B_RENDER;
  3445.  
  3446.    return;
  3447. }
  3448.  
  3449. /* ------------------------------------------------------------------- */
  3450. /* upd_close_fscomp: Deinitialize Component-Floyd-Steinberg            */
  3451. /* ------------------------------------------------------------------- */
  3452.  
  3453. private void
  3454. upd_close_fscomp(upd_device *udev)
  3455. {
  3456.    const upd_p upd = udev->upd;
  3457.    int icomp;
  3458.  
  3459. #if UPD_MESSAGES & UPD_M_FSBUF
  3460.    if(upd && (upd->flags & B_RENDER)) {
  3461.  
  3462.       for(icomp = 0; icomp < upd->ncomp; ++icomp) {
  3463.          updcomp_p comp = upd->valptr[icomp];
  3464.          if(!comp) continue;
  3465.          if(!comp->spotsize) continue;
  3466.          fprintf(stderr,"%d. Component: %6.3f <= error <= %6.3f\n",
  3467.              icomp+1,
  3468.              (double) fs_emin[icomp] / (double) comp->spotsize,
  3469.              (double) fs_emax[icomp] / (double) comp->spotsize);
  3470.       }
  3471.  
  3472.    }
  3473. #endif
  3474.  
  3475.    for(icomp = 0; UPD_VALPTR_MAX > icomp; ++icomp) {
  3476.       if(!upd->valptr[icomp]) continue;
  3477.       gs_free(upd->valptr[icomp],1,sizeof(updcomp_t),"upd/fscomp");
  3478.       upd->valptr[icomp] = NULL;
  3479.    }
  3480. }
  3481.  
  3482. /* ------------------------------------------------------------------- */
  3483. /* upd_fscomp: Apply Floyd-Steinberg to each component                 */
  3484. /* ------------------------------------------------------------------- */
  3485.  
  3486. /**
  3487.    With UPD_M_FSBUF Max/Min-Values for the Errors are computed
  3488. */
  3489. #if   UPD_MESSAGES & UPD_M_FSBUF
  3490. #define FS_M_ROWERR(I)                                        \
  3491.            if(fs_emin[I] > rowerr[I]) fs_emin[I] = rowerr[I]; \
  3492.            if(fs_emax[I] < rowerr[I]) fs_emax[I] = rowerr[I];
  3493. #else
  3494. #define FS_M_ROWERR(I) ;
  3495. #endif
  3496. /**
  3497.    FS_GOAL computes the desired Pixel-Value
  3498. */
  3499. #define FS_GOAL(Raw,I)                                                     \
  3500.    pixel[I] = (int32)(Raw) * comp[I]->scale +    comp[I]->offset           \
  3501.             + rowerr[I]  + colerr[I] -       ((colerr[I]+4)>>3);           \
  3502.    if(         pixel[I] < 0)                    pixel[I] = 0;              \
  3503.    else if(    pixel[I] >    comp[I]->spotsize) pixel[I] = comp[I]->spotsize;
  3504.  
  3505. /*
  3506.  *    Distribute the error:   prev  now   next
  3507.  *                                   X    7/16 Y
  3508.  *                            3/16  5/16  1/16 Y+1
  3509.  */
  3510. #define FS_DIST(I)                                                    \
  3511.    if(!first) rowerr[I-dir] += ((3*pixel[I]+8)>>4); /* 3/16 */        \
  3512.               rowerr[I    ]  = ((5*pixel[I]  )>>4)  /* 5/16 */        \
  3513.                              + (( colerr[I]+4)>>3); /* 1/16 (rest) */ \
  3514.               colerr[I    ]  = pixel[I]             /* 8/16 (neu) */  \
  3515.                              - ((5*pixel[I]  )>>4)                    \
  3516.                              - ((3*pixel[I]+8)>>4);
  3517. /**
  3518.    S_FSTEP   adjusts the Indices (rowerr, bit and iword)
  3519. */
  3520. #define S_FSTEP                                \
  3521.    rowerr += dir;                              \
  3522.    first   = false;                            \
  3523.    if(0 > dir) { /* Reverse */                 \
  3524.       if(!(bit <<= 1)) { bit = 0x01; ibyte--; }\
  3525.    } else {      /* Forward */                 \
  3526.       if(!(bit >>= 1)) { bit = 0x80; ibyte++; }\
  3527.    }             /* Inc/Dec Bit */
  3528.  
  3529. private int
  3530. upd_fscomp(upd_p upd)
  3531. {
  3532.    const updscan_p  scan    = upd->scnbuf[upd->yscnbuf & upd->scnmsk];
  3533.    const updcomp_p *comp    = (updcomp_p *) upd->valptr;
  3534.    int32 *const     pixel  = upd->valbuf;
  3535.    int32 *const     colerr = pixel  + upd->ncomp;
  3536.    int32           *rowerr = colerr + upd->ncomp;
  3537.    int              pwidth = upd->rwidth;
  3538.    int              dir,ibyte;
  3539.    int              iblack,bblack,pxlset;
  3540.    uint32       ci;
  3541.    byte         bit;
  3542.    bool         first = true;
  3543. /*
  3544.  * Erase the component-Data
  3545.  */
  3546.    switch(upd->ncomp) {
  3547.      case 4:  memset(scan[3].bytes,0,upd->nbytes);
  3548.      case 3:  memset(scan[2].bytes,0,upd->nbytes);
  3549.               memset(scan[1].bytes,0,upd->nbytes);
  3550.      default: memset(scan[0].bytes,0,upd->nbytes);
  3551.    }
  3552. /*
  3553.  * determine the direction
  3554.  */
  3555.    if(upd->flags &   B_REVDIR) { /* This one reverse */
  3556.  
  3557.       if(upd->flags & B_YFLIP) {
  3558.          dir     = upd->ncomp;
  3559.          bit     = 0x80;
  3560.          ibyte   = 0;
  3561.       } else {
  3562.          dir     =  -upd->ncomp;
  3563.          rowerr +=   upd->ncomp * (pwidth-1);
  3564.          bit     =   0x80 >>     ((pwidth-1) & 7);
  3565.          ibyte   =                (pwidth-1) >> 3;
  3566.       }
  3567.  
  3568.       if(!(upd->flags & B_FSWHITE)) {
  3569.          upd_pxlfwd(upd);
  3570.          while((0 < pwidth) && !upd_pxlget(upd)) pwidth--;
  3571.       }
  3572.  
  3573.       upd_pxlrev(upd);
  3574.  
  3575.    } else {                       /* This one forward */
  3576.  
  3577.       if(upd->flags & B_YFLIP) {
  3578.          dir     =  -upd->ncomp;
  3579.          rowerr +=   upd->ncomp * (pwidth-1);
  3580.          bit     =   0x80 >>     ((pwidth-1) & 7);
  3581.          ibyte   =                (pwidth-1) >> 3;
  3582.       } else {
  3583.          dir     = upd->ncomp;
  3584.          bit     = 0x80;
  3585.          ibyte   = 0;
  3586.       }
  3587.  
  3588.       if(!(upd->flags & B_FSWHITE)) {
  3589.          upd_pxlrev(upd);
  3590.          while((0 < pwidth) && !upd_pxlget(upd)) pwidth--;
  3591.       }
  3592.  
  3593.       upd_pxlfwd(upd);
  3594.  
  3595.    }                              /* reverse or forward */
  3596. /*
  3597.  * Toggle Direction, if not fixed
  3598.  */
  3599.    if(!(upd->flags & B_FIXDIR)) upd->flags ^= B_REVDIR;
  3600. /*
  3601.  * Skip over leading white-space
  3602.  */
  3603.    if(!(upd->flags & B_FSWHITE)) {
  3604.       upd_proc_pxlget((*fun)) = upd->pxlget;
  3605.       byte             *ptr   = upd->pxlptr;
  3606.       while((0 < pwidth) && !upd_pxlget(upd)) {
  3607.          pwidth--;
  3608.          fun = upd->pxlget;
  3609.          ptr = upd->pxlptr;
  3610.          S_FSTEP
  3611.       }
  3612.       upd->pxlget = fun;
  3613.       upd->pxlptr = ptr;
  3614.    }
  3615. /*
  3616.  * Set iblack, if black-reduction is active
  3617.  */
  3618.   iblack = -1;
  3619.   bblack =  0;
  3620.   if((4 == upd->ncomp) && (B_REDUCEK & upd->flags)) {
  3621.     iblack = upd->cmap[0].comp;
  3622.     bblack = 1<<iblack;
  3623.   }
  3624. /*
  3625.  * Process all Pixels
  3626.  */
  3627.    first = true;
  3628.    while(0 < pwidth--) {
  3629. /*
  3630.  *    Execute FS-Algorithm for each active component
  3631.  */
  3632.       pxlset = 0;
  3633.       ci = upd_pxlget(upd);
  3634.       switch(upd->ncomp) {
  3635.          case 4:  FS_M_ROWERR(3)
  3636.                   FS_GOAL(comp[3]->bitmsk & (ci >> comp[3]->bitshf),3)
  3637.                   if(pixel[3] >  comp[3]->threshold) { /* "Fire" */
  3638.                      pixel[3] -= comp[3]->spotsize;
  3639.                       scan[3].bytes[ibyte] |= bit;
  3640.                       pxlset  |= 8;
  3641.                   }                                    /* "Fire" */
  3642.                   FS_DIST(3)
  3643.  
  3644.          case 3:  FS_M_ROWERR(2)
  3645.                   FS_GOAL(comp[2]->bitmsk & (ci >> comp[2]->bitshf),2)
  3646.                   if(pixel[2] >  comp[2]->threshold) { /* "Fire" */
  3647.                      pixel[2] -= comp[2]->spotsize;
  3648.                       scan[2].bytes[ibyte] |= bit;
  3649.                       pxlset  |= 4;
  3650.                   }                                    /* "Fire" */
  3651.                   FS_DIST(2)
  3652.  
  3653.                   FS_M_ROWERR(1)
  3654.                   FS_GOAL(comp[1]->bitmsk & (ci >> comp[1]->bitshf),1)
  3655.                   if(pixel[1] >  comp[1]->threshold) { /* "Fire" */
  3656.                      pixel[1] -= comp[1]->spotsize;
  3657.                       scan[1].bytes[ibyte] |= bit;
  3658.                       pxlset  |= 2;
  3659.                   }                                    /* "Fire" */
  3660.                   FS_DIST(1)
  3661.  
  3662.          default: FS_M_ROWERR(0)
  3663.                   FS_GOAL(comp[0]->bitmsk & (ci >> comp[0]->bitshf),0)
  3664.                   if(pixel[0] >  comp[0]->threshold) { /* "Fire" */
  3665.                      pixel[0] -= comp[0]->spotsize;
  3666.                       scan[0].bytes[ibyte] |= bit;
  3667.                       pxlset  |= 1;
  3668.                   }                                    /* "Fire" */
  3669.                   FS_DIST(0)
  3670.       }
  3671. /*
  3672.  *    Black-Reduction
  3673.  */
  3674.       if(bblack) {
  3675.         if(pxlset & bblack) pxlset |= 15;
  3676.         switch(pxlset) {
  3677.           case  0:
  3678.           case  1:
  3679.           case  2:
  3680.           case  4:
  3681.           case  8:
  3682.           case  3:
  3683.           case  5:
  3684.           case  9:
  3685.           case  6:
  3686.           case 10:
  3687.           case 12:
  3688.             break;
  3689.           default:
  3690.             scan[0].bytes[ibyte]      &= ~bit;
  3691.             scan[1].bytes[ibyte]      &= ~bit;
  3692.             scan[2].bytes[ibyte]      &= ~bit;
  3693.             scan[3].bytes[ibyte]      &= ~bit;
  3694.             scan[iblack].bytes[ibyte] |=  bit;
  3695.           break;
  3696.         }
  3697.       }
  3698. /*
  3699.  *    Adjust rowerr, bit & iword, depending on direction
  3700.  */
  3701.       S_FSTEP
  3702.    }
  3703. /*
  3704.  * Finally call the limits-Routine
  3705.  */
  3706.    if(0 < upd->nlimits) upd_limits(upd,true);
  3707.    return 0;
  3708. }
  3709.  
  3710. /* ------------------------------------------------------------------- */
  3711. /* upd_open_fscmyk: Initialize Component-Floyd-Steinberg               */
  3712. /* ------------------------------------------------------------------- */
  3713.  
  3714. private void
  3715. upd_open_fscmyk(upd_device *udev)
  3716. {
  3717.    const upd_p upd = udev->upd;
  3718.  
  3719.    upd_open_fscomp(udev);
  3720.  
  3721.    if((B_RENDER & upd->flags) &&
  3722.       (4 == upd->ncomp) &&
  3723.       (8 <= upd->cmap[0].bits) && (24 == upd->cmap[0].bitshf) &&
  3724.       (8 <= upd->cmap[1].bits) && (16 == upd->cmap[1].bitshf) &&
  3725.       (8 <= upd->cmap[2].bits) && ( 8 == upd->cmap[2].bitshf) &&
  3726.       (8 <= upd->cmap[3].bits) && ( 0 == upd->cmap[3].bitshf)   ) {
  3727.       upd->render = upd_fscmyk;
  3728.    } else {
  3729.       upd->flags &= ~B_RENDER;
  3730.    }
  3731.  
  3732. }
  3733.  
  3734. /* ------------------------------------------------------------------- */
  3735. /* upd_fscmyk: 32 Bit, K-CMY-Order Dithering                           */
  3736. /* ------------------------------------------------------------------- */
  3737.  
  3738. private int
  3739. upd_fscmyk(upd_p upd)
  3740. {
  3741.    const updscan_p  scan   = upd->scnbuf[upd->yscnbuf & upd->scnmsk];
  3742.    int32 *const     pixel  = upd->valbuf;
  3743.    const updcomp_p *comp   = (updcomp_p *) upd->valptr;
  3744.    int32 *const     colerr = pixel  + 4;
  3745.    int32           *rowerr = colerr + 4;
  3746.    int32            pwidth = upd->rwidth;
  3747.    int              dir,ibyte;
  3748.    byte             bit,*data;
  3749.    bool             first = false;
  3750. /*
  3751.  * Erase the component-Data
  3752.  */
  3753.    memset(scan[0].bytes,0,upd->nbytes);
  3754.    memset(scan[1].bytes,0,upd->nbytes);
  3755.    memset(scan[2].bytes,0,upd->nbytes);
  3756.    memset(scan[3].bytes,0,upd->nbytes);
  3757.  
  3758. /*
  3759.  * determine the direction
  3760.  */
  3761.    if(upd->flags &   B_REVDIR) { /* This one reverse */
  3762.  
  3763.       if(!(upd->flags & B_FSWHITE)) {
  3764.          data = upd->gsscan;
  3765.          while(0 < pwidth && !*(uint32 *)data) pwidth--, data += 4;
  3766.          if(0 >= pwidth) {
  3767.             if(0 < upd->nlimits) upd_limits(upd,false);
  3768.             return 0;
  3769.          }
  3770.       }
  3771.       
  3772.       data        = upd->gsscan + 4 * (upd->rwidth-1);
  3773.  
  3774.    } else {                          /* This one forward */
  3775.  
  3776.       if(!(upd->flags & B_FSWHITE)) {
  3777.          data = upd->gsscan + 4 * (upd->rwidth-1);
  3778.          while(0 < pwidth && !*(uint32 *)data) pwidth--, data -= 4;
  3779.          if(0 >= pwidth) {
  3780.             if(0 < upd->nlimits) upd_limits(upd,false);
  3781.             return 0;
  3782.          }
  3783.       }
  3784.  
  3785.       data        = upd->gsscan;
  3786.  
  3787.    }                              /* reverse or forward */
  3788. /*
  3789.  * Bits depend on FLIP & Direction
  3790.  */
  3791.    if(!(B_REVDIR & upd->flags) == !(B_YFLIP  & upd->flags)) {
  3792.       dir         = 4;
  3793.       bit         = 0x80;
  3794.       ibyte       = 0;
  3795.    } else {
  3796.       dir         =  -4;
  3797.       rowerr     +=   4 *             (upd->rwidth-1);
  3798.       bit         =   0x80 >>        ((upd->rwidth-1) & 7);
  3799.       ibyte       =                   (upd->rwidth-1) >> 3;
  3800.    }
  3801.  
  3802. /*
  3803.  * Toggle Direction, if not fixed
  3804.  */
  3805.    if(!(upd->flags & B_FIXDIR)) upd->flags ^= B_REVDIR;
  3806. /*
  3807.  * Skip over leading white-space
  3808.  */
  3809.    if(!(upd->flags & B_FSWHITE)) {
  3810.       while(0 < pwidth && !*((uint32 *)data)) {
  3811.          pwidth--;
  3812.          if(B_YFLIP  & upd->flags) data -= dir;
  3813.          else                      data += dir;
  3814.          S_FSTEP
  3815.       }
  3816.    }
  3817. /*
  3818.  * Process all Pixels
  3819.  */
  3820.    first = true;
  3821.    while(0 < pwidth--) {
  3822. /*
  3823.  *    Compute the Black-Value first
  3824.  */
  3825.       FS_M_ROWERR(upd->cmap[0].comp) FS_GOAL(data[0],upd->cmap[0].comp);
  3826.  
  3827. /*
  3828.  *    Decide wether this is a color value
  3829.  */
  3830.       if(data[1] || data[2] || data[3]) {
  3831.  
  3832.          FS_M_ROWERR(upd->cmap[1].comp) FS_GOAL(data[1],upd->cmap[1].comp)
  3833.          FS_M_ROWERR(upd->cmap[2].comp) FS_GOAL(data[2],upd->cmap[2].comp)
  3834.          FS_M_ROWERR(upd->cmap[3].comp) FS_GOAL(data[3],upd->cmap[3].comp)
  3835. /*
  3836.  *       if black fires, then all other components fire logically too
  3837.  */
  3838.          if(pixel[upd->cmap[0].comp] > comp[upd->cmap[0].comp]->threshold) {
  3839.  
  3840.             pixel[0] -= comp[0]->spotsize;
  3841.             pixel[1] -= comp[1]->spotsize;
  3842.             pixel[2] -= comp[2]->spotsize;
  3843.             pixel[3] -= comp[3]->spotsize;
  3844.             scan[upd->cmap[0].comp].bytes[ibyte] |= bit;
  3845.  
  3846. /*
  3847.  *       if black is below threshold, only components with larger data-values
  3848.  *       are allowed to fire
  3849.  */
  3850.          } else {                                 /* Restricted firing */
  3851.  
  3852.             if(( data[0] < data[1]) &&
  3853.                (pixel[upd->cmap[1].comp] >
  3854.                  comp[upd->cmap[1].comp]->threshold)) { /* "Fire" */
  3855.                 pixel[upd->cmap[1].comp] -= comp[upd->cmap[1].comp]->spotsize;
  3856.                  scan[upd->cmap[1].comp].bytes[ibyte] |= bit;
  3857.             }                                           /* "Fire" */
  3858.  
  3859.             if(( data[0] < data[2]) &&
  3860.                (pixel[upd->cmap[2].comp] >
  3861.                  comp[upd->cmap[2].comp]->threshold)) { /* "Fire" */
  3862.                 pixel[upd->cmap[2].comp] -= comp[upd->cmap[2].comp]->spotsize;
  3863.                  scan[upd->cmap[2].comp].bytes[ibyte] |= bit;
  3864.             }                                           /* "Fire" */
  3865.  
  3866.             if(( data[0] < data[3]) &&
  3867.                (pixel[upd->cmap[3].comp] >
  3868.                  comp[upd->cmap[3].comp]->threshold)) { /* "Fire" */
  3869.                 pixel[upd->cmap[3].comp] -= comp[upd->cmap[3].comp]->spotsize;
  3870.                  scan[upd->cmap[3].comp].bytes[ibyte] |= bit;
  3871.             }                                           /* "Fire" */
  3872.  
  3873.          }                                        /* Fire-Mode */
  3874.  
  3875. /*
  3876.  * Handle Color-Errors
  3877.  */
  3878.          FS_DIST(upd->cmap[3].comp)
  3879.          FS_DIST(upd->cmap[2].comp)
  3880.          FS_DIST(upd->cmap[1].comp)
  3881.  
  3882.       } else if(pixel[upd->cmap[0].comp] > comp[upd->cmap[0].comp]->threshold) {
  3883.                  scan[upd->cmap[0].comp].bytes[ibyte] |= bit;
  3884.                 pixel[upd->cmap[0].comp] -= comp[upd->cmap[0].comp]->spotsize;
  3885.       }
  3886.  
  3887.       FS_DIST(upd->cmap[0].comp)
  3888. /*
  3889.  *    Adjust bit & iword, depending on direction
  3890.  */
  3891.       S_FSTEP
  3892.       if(upd->flags & B_YFLIP) data -= dir;
  3893.       else                     data += dir;
  3894.    }
  3895. /*
  3896.  * Finally call the limits-Routine
  3897.  */
  3898.    if(0 < upd->nlimits) upd_limits(upd,true);
  3899.    return 0;
  3900. }
  3901.  
  3902. /* ------------------------------------------------------------------- */
  3903. /* upd_open_fscmy_k: Initialize for CMY_K Printing                     */
  3904. /* ------------------------------------------------------------------- */
  3905.  
  3906. private void
  3907. upd_open_fscmy_k(upd_device *udev)
  3908. {
  3909.    const upd_p upd = udev->upd;
  3910.  
  3911.    upd_open_fscomp(udev);
  3912.  
  3913.    if((B_RENDER & upd->flags) &&
  3914.       (4 == upd->ncomp)) {
  3915.       upd->render = upd_fscmy_k;
  3916.    } else {
  3917.       upd->flags &= ~B_RENDER;
  3918.    }
  3919.  
  3920. }
  3921.  
  3922. /* ------------------------------------------------------------------- */
  3923. /* upd_fscmy_k: CMY_K rendering                                        */
  3924. /* ------------------------------------------------------------------- */
  3925.  
  3926. private int
  3927. upd_fscmy_k(upd_p upd)
  3928. {
  3929.    const updscan_p  scan    = upd->scnbuf[upd->yscnbuf & upd->scnmsk];
  3930.    const updcomp_p *comp    = (updcomp_p *) upd->valptr;
  3931.    int32 *const     pixel  = upd->valbuf;
  3932.    int32 *const     colerr = pixel  + upd->ncomp;
  3933.    int32           *rowerr = colerr + upd->ncomp;
  3934.    int              pwidth = upd->rwidth;
  3935.    int              dir,ibyte;
  3936.    uint32       ci;
  3937.    byte         bit;
  3938.    bool         first = true;
  3939. /*
  3940.  * Erase the component-Data
  3941.  */
  3942.    memset(scan[3].bytes,0,upd->nbytes);
  3943.    memset(scan[2].bytes,0,upd->nbytes);
  3944.    memset(scan[1].bytes,0,upd->nbytes);
  3945.    memset(scan[0].bytes,0,upd->nbytes);
  3946. /*
  3947.  * determine the direction
  3948.  */
  3949.    if(upd->flags &   B_REVDIR) { /* This one reverse */
  3950.  
  3951.       if(upd->flags & B_YFLIP) {
  3952.          dir     = 4;
  3953.          bit     = 0x80;
  3954.          ibyte   = 0;
  3955.       } else {
  3956.          dir     =  -4;
  3957.          rowerr +=   4 * (pwidth-1);
  3958.          bit     =   0x80 >>     ((pwidth-1) & 7);
  3959.          ibyte   =                (pwidth-1) >> 3;
  3960.       }
  3961.  
  3962.       if(!(upd->flags & B_FSWHITE)) {
  3963.          upd_pxlfwd(upd);
  3964.          while((0 < pwidth) && !upd_pxlget(upd)) pwidth--;
  3965.       }
  3966.  
  3967.       upd_pxlrev(upd);
  3968.  
  3969.    } else {                       /* This one forward */
  3970.  
  3971.       if(upd->flags & B_YFLIP) {
  3972.          dir     =  -4;
  3973.          rowerr +=   4          * (pwidth-1);
  3974.          bit     =   0x80 >>     ((pwidth-1) & 7);
  3975.          ibyte   =                (pwidth-1) >> 3;
  3976.       } else {
  3977.          dir     = 4;
  3978.          bit     = 0x80;
  3979.          ibyte   = 0;
  3980.       }
  3981.  
  3982.       if(!(upd->flags & B_FSWHITE)) {
  3983.          upd_pxlrev(upd);
  3984.          while((0 < pwidth) && !upd_pxlget(upd)) pwidth--;
  3985.       }
  3986.  
  3987.       upd_pxlfwd(upd);
  3988.  
  3989.    }                              /* reverse or forward */
  3990. /*
  3991.  * Toggle Direction, if not fixed
  3992.  */
  3993.    if(!(upd->flags & B_FIXDIR)) upd->flags ^= B_REVDIR;
  3994. /*
  3995.  * Skip over leading white-space
  3996.  */
  3997.    if(!(upd->flags & B_FSWHITE)) {
  3998.       upd_proc_pxlget((*fun)) = upd->pxlget;
  3999.       byte             *ptr   = upd->pxlptr;
  4000.       while((0 < pwidth) && !upd_pxlget(upd)) {
  4001.          pwidth--;
  4002.          fun = upd->pxlget;
  4003.          ptr = upd->pxlptr;
  4004.          S_FSTEP
  4005.       }
  4006.       upd->pxlget = fun;
  4007.       upd->pxlptr = ptr;
  4008.    }
  4009. /*
  4010.  * Process all Pixels
  4011.  */
  4012.    first = true;
  4013.    while(0 < pwidth--) {
  4014.  
  4015. /*    get the Pixel-Value */
  4016.  
  4017.       ci = upd_pxlget(upd);
  4018.  
  4019. /*    process all components */
  4020.  
  4021.       FS_M_ROWERR(0) FS_GOAL(comp[0]->bitmsk & (ci >> comp[0]->bitshf),0)
  4022.       FS_M_ROWERR(1) FS_GOAL(comp[1]->bitmsk & (ci >> comp[1]->bitshf),1)
  4023.       FS_M_ROWERR(2) FS_GOAL(comp[2]->bitmsk & (ci >> comp[2]->bitshf),2)
  4024.       FS_M_ROWERR(3) FS_GOAL(comp[3]->bitmsk & (ci >> comp[3]->bitshf),3)
  4025.  
  4026.       if(pixel[0] >  comp[0]->threshold) { /* Black fires */
  4027.  
  4028.         pixel[0]             -= comp[0]->spotsize;
  4029.         scan[0].bytes[ibyte] |= bit;
  4030.  
  4031.       } else {                             /* Colors may fire */
  4032.  
  4033.          if((pixel[1] <= comp[1]->threshold) ||
  4034.             (pixel[2] <= comp[2]->threshold) ||
  4035.             (pixel[3] <= comp[3]->threshold)   ) { /* Really a Color */
  4036.  
  4037.             if(pixel[1] >               comp[1]->threshold) {
  4038.                pixel[1]              -= comp[1]->spotsize;
  4039.                 scan[1].bytes[ibyte] |= bit;
  4040.             }
  4041.  
  4042.             if(pixel[2] >               comp[2]->threshold) {
  4043.                pixel[2]              -= comp[2]->spotsize;
  4044.                 scan[2].bytes[ibyte] |= bit;
  4045.             }
  4046.  
  4047.             if(pixel[3] >               comp[3]->threshold) {
  4048.                pixel[3]              -= comp[3]->spotsize;
  4049.                 scan[3].bytes[ibyte] |= bit;
  4050.             }
  4051.  
  4052.          } else {
  4053.             pixel[1]              -= comp[1]->spotsize;
  4054.             pixel[2]              -= comp[2]->spotsize;
  4055.             pixel[3]              -= comp[3]->spotsize;
  4056.             scan[0].bytes[ibyte] |= bit;
  4057.          }
  4058.       }
  4059.  
  4060.       FS_DIST(0)
  4061.       FS_DIST(1)
  4062.       FS_DIST(2)
  4063.       FS_DIST(3)
  4064.  
  4065. /*
  4066.  *    Adjust rowerr, bit & iword, depending on direction
  4067.  */
  4068.       S_FSTEP
  4069.    }
  4070. /*
  4071.  * Finally call the limits-Routine
  4072.  */
  4073.    if(0 < upd->nlimits) upd_limits(upd,true);
  4074.    return 0;
  4075. }
  4076.  
  4077. /* ------------------------------------------------------------------- */
  4078. /* upd_open_writer: Initialize rendering                               */
  4079. /* ------------------------------------------------------------------- */
  4080.  
  4081. private int
  4082. upd_open_writer(upd_device *udev)
  4083. {
  4084.    const upd_p upd                 = udev->upd;
  4085.    bool        success             = true;
  4086.  
  4087.  
  4088. /** Reset the crucial values */
  4089.    upd->start_writer = NULL;
  4090.    upd->writer       = NULL;
  4091.    upd->scnbuf       = NULL;
  4092.    upd->nscnbuf      = 0;
  4093.    upd->nbytes       = 0;
  4094.    upd->nlimits      = 0;
  4095.    upd->outbuf       = NULL;
  4096.    upd->noutbuf      = 0;
  4097.  
  4098. /** Rendering should be succesfully initialized */
  4099.    if(B_RENDER != ((B_RENDER | B_ERROR) & upd->flags))
  4100.       success = false;
  4101.  
  4102. /** Create number of components */
  4103.    upd->ocomp = upd->ncomp;
  4104.    if(0 < upd->ints[I_OCOMP]) upd->ocomp = upd->ints[I_OCOMP];
  4105.  
  4106. /** Massage some Parameters */
  4107.    if(success) {
  4108.  
  4109. /*    Make sure, that Pass & Pin-Numbers are at least 1 */
  4110.       if(1 >  upd->ints[I_NYPASS]) upd->ints[I_NYPASS] = 1;
  4111.       if(1 >  upd->ints[I_NXPASS]) upd->ints[I_NXPASS] = 1;
  4112.       if(1 >  upd->ints[I_PINS2WRITE]) upd->ints[I_PINS2WRITE] = 1;
  4113.  
  4114.       if((upd->ints[I_NXPASS] * upd->ints[I_NYPASS]) > upd->ints[I_NPASS])
  4115.          upd->ints[I_NPASS] = upd->ints[I_NXPASS] * upd->ints[I_NYPASS];
  4116.  
  4117. /*    Create Default noWeave-Feeds */
  4118.  
  4119.       if(upd->ints[I_NPASS] > upd->int_a[IA_STD_DY].size) {
  4120.          int ix,iy,*ip;
  4121.          UPD_MM_DEL_PARAM(upd->int_a[IA_STD_DY]);
  4122.          UPD_MM_GET_ARRAY(ip,upd->ints[I_NPASS]);
  4123.          upd->int_a[IA_STD_DY].data = ip;
  4124.          upd->int_a[IA_STD_DY].size = upd->ints[I_NPASS];
  4125.  
  4126.          for(iy = 1; iy < upd->ints[I_NYPASS]; ++iy) {
  4127.             for(ix = 1; ix < upd->ints[I_NXPASS]; ++ix) *ip++ = 0;
  4128.             *ip++ = 1;
  4129.          }
  4130.          for(ix = 1; ix < upd->ints[I_NXPASS]; ++ix) *ip++ = 0;
  4131.          *ip = upd->ints[I_NYPASS] * upd->ints[I_PINS2WRITE]
  4132.              - upd->ints[I_NYPASS] + 1;
  4133.  
  4134.          upd->ints[I_BEG_Y] = 0;
  4135.          upd->ints[I_END_Y] = upd->ints[I_PHEIGHT] ? 
  4136.                               upd->ints[I_PHEIGHT] : upd->gsheight;
  4137.       }
  4138.  
  4139. /*    Adjust BEG_Y */
  4140.       if(0 >= upd->ints[I_BEG_Y]) {
  4141.          if(0 <  upd->int_a[IA_BEG_DY].size) {
  4142.             int i,sum = 0;
  4143.             for(i = 0; i < upd->int_a[IA_BEG_DY].size; ++i)
  4144.                sum +=  upd->int_a[IA_BEG_DY].data[i];
  4145.             upd->ints[I_BEG_Y] = sum;
  4146.          } else {
  4147.             upd->ints[I_BEG_Y] = 0;
  4148.          }
  4149.       }
  4150.  
  4151. /*    Adjust END_Y */
  4152. /*    Arrgh, I knew, why I refused to provide defaults for crucial */
  4153. /*    parameters in uniprint. But o.k. it's nice for size-changing */
  4154. /*    PostScript-Code. Nevertheless, it's still not perfect.       */
  4155.  
  4156.       if(0 >= upd->int_a[IA_ENDTOP].size ||
  4157.          0 >= upd->int_a[IA_END_DY].size   ) upd->ints[I_END_Y] =
  4158.          upd->ints[I_PHEIGHT] ? upd->ints[I_PHEIGHT] : upd->gsheight;
  4159.  
  4160.       if(0 >= upd->ints[I_END_Y]) upd->ints[I_END_Y] = upd->ints[I_PHEIGHT] ?
  4161.         upd->ints[I_PHEIGHT] : upd->gsheight;
  4162.  
  4163.  
  4164. /*    Create Default X-Passes */
  4165.  
  4166.       if(0 >= upd->int_a[IA_STD_IX].size) {
  4167.          int ix,i,*ip;
  4168.          UPD_MM_DEL_PARAM(upd->int_a[IA_STD_IX]);
  4169.          UPD_MM_GET_ARRAY(ip,upd->int_a[IA_STD_DY].size);
  4170.          upd->int_a[IA_STD_IX].data = ip;
  4171.          upd->int_a[IA_STD_IX].size = upd->int_a[IA_STD_DY].size;
  4172.  
  4173.          for(i = 0, ix = 0; i < upd->int_a[IA_STD_IX].size; ++i) {
  4174.             *ip++ = ix++;
  4175.             if(ix == upd->ints[I_NXPASS]) ix = 0;
  4176.          }
  4177.       }
  4178.  
  4179.       if((0 >= upd->int_a[IA_BEG_IX].size) &&
  4180.          (0 <  upd->int_a[IA_BEG_DY].size)   ) {
  4181.          int ix,i,*ip;
  4182.          UPD_MM_DEL_PARAM(upd->int_a[IA_BEG_IX]);
  4183.          UPD_MM_GET_ARRAY(ip,upd->int_a[IA_BEG_DY].size);
  4184.          upd->int_a[IA_BEG_IX].data = ip;
  4185.          upd->int_a[IA_BEG_IX].size = upd->int_a[IA_BEG_DY].size;
  4186.  
  4187.          for(i = 0, ix = 0; i < upd->int_a[IA_BEG_IX].size; ++i) {
  4188.             *ip++ = ix++;
  4189.             if(ix == upd->ints[I_NXPASS]) ix = 0;
  4190.          }
  4191.       }
  4192.  
  4193.       if((0 >= upd->int_a[IA_END_IX].size) &&
  4194.          (0 <  upd->int_a[IA_END_DY].size)   ) {
  4195.          int ix,i,*ip;
  4196.          UPD_MM_DEL_PARAM(upd->int_a[IA_END_IX]);
  4197.          UPD_MM_GET_ARRAY(ip,upd->int_a[IA_END_DY].size);
  4198.          upd->int_a[IA_END_IX].data = ip;
  4199.          upd->int_a[IA_END_IX].size = upd->int_a[IA_END_DY].size;
  4200.  
  4201.          for(i = 0, ix = 0; i < upd->int_a[IA_END_IX].size; ++i) {
  4202.             *ip++ = ix++;
  4203.             if(ix == upd->ints[I_NXPASS]) ix = 0;
  4204.          }
  4205.       }
  4206.    }
  4207.  
  4208.    if(upd->ints[I_NPASS] > upd->int_a[IA_STD_DY].size) {
  4209. #if UPD_MESSAGES & UPD_M_WARNING
  4210.       fprintf(stderr,
  4211.         "upd_open_writer: Only %d instead of %d normal Feeds\n",
  4212.         (int) upd->int_a[IA_STD_DY].size,upd->ints[I_NPASS]);
  4213. #endif
  4214.       success = false;
  4215.  
  4216.    } else if(upd->int_a[IA_STD_IX].size < upd->int_a[IA_STD_DY].size) {
  4217. #if UPD_MESSAGES & UPD_M_WARNING
  4218.       fprintf(stderr,
  4219.         "upd_open_writer: Only %d instead of %d normal Xstarts\n",
  4220.         (int) upd->int_a[IA_STD_IX].size,
  4221.         (int) upd->int_a[IA_STD_DY].size);
  4222. #endif
  4223.       success = false;
  4224.    }
  4225.  
  4226. /** The sum of Values in STD_DY should equal NYPASS * PINS2WRITE (diagnostic) */
  4227.  
  4228. #if UPD_MESSAGES & UPD_M_WARNING
  4229.    if(success) {
  4230.       int i,sum = 0;
  4231.       for(i = 0; upd->ints[I_NPASS] > i; ++i)
  4232.          sum += upd->int_a[IA_STD_DY].data[i];
  4233.       if((upd->ints[I_NYPASS]*upd->ints[I_PINS2WRITE]) != sum)
  4234.          fprintf(stderr,
  4235.          "upd_open_writer: Sum of normal Feeds is %d rather than %d\n",
  4236.          sum,upd->ints[I_NYPASS]*upd->ints[I_PINS2WRITE]);
  4237.    }
  4238. #endif
  4239.  
  4240.    if(upd->int_a[IA_BEG_IX].size < upd->int_a[IA_BEG_DY].size) {
  4241. #if UPD_MESSAGES & UPD_M_WARNING
  4242.       fprintf(stderr,
  4243.         "upd_open_writer: Only %d instead of %d initial Xstarts\n",
  4244.         (int) upd->int_a[IA_BEG_IX].size,
  4245.         (int) upd->int_a[IA_BEG_DY].size);
  4246. #endif
  4247.       success = false;
  4248.    }
  4249.  
  4250.    if(upd->int_a[IA_BEGBOT].size < upd->int_a[IA_BEG_DY].size) {
  4251. #if UPD_MESSAGES & UPD_M_WARNING
  4252.       fprintf(stderr,
  4253.         "upd_open_writer: Only %d instead of %d initial Pins\n",
  4254.         (int) upd->int_a[IA_BEGBOT].size,
  4255.         (int) upd->int_a[IA_BEG_DY].size);
  4256. #endif
  4257.       success = false;
  4258.  
  4259.    } else {
  4260.  
  4261.       int i;
  4262.       for(i = 0; i < upd->int_a[IA_BEG_DY].size; ++i)
  4263.          if((upd->int_a[IA_BEGBOT].data[i] > upd->ints[I_PINS2WRITE]) ||
  4264.             (upd->int_a[IA_BEGBOT].data[i] < 0                      )   ) break;
  4265.  
  4266.       if(i < upd->int_a[IA_BEG_DY].size) {
  4267. #if UPD_MESSAGES & UPD_M_WARNING
  4268.          fprintf(stderr,
  4269.            "upd_open_writer: Only %d is invalid initial Pins\n",
  4270.            upd->int_a[IA_BEGBOT].data[i]);
  4271. #endif
  4272.          success = false;
  4273.       }
  4274.    }
  4275.  
  4276.  
  4277. /** The sum of Values in BEG_DY should equal BEG_Y */
  4278.  
  4279. #if UPD_MESSAGES & UPD_M_WARNING
  4280.    if(success) {
  4281.       int i,sum = 0;
  4282.       for(i = 0;  upd->int_a[IA_BEG_DY].size > i; ++i)
  4283.          sum += upd->int_a[IA_BEG_DY].data[i];
  4284.       if(upd->ints[I_BEG_Y] != sum)
  4285.          fprintf(stderr,
  4286.          "upd_open_writer: Sum of initial Feeds is %d rather than %d\n",
  4287.          sum,upd->ints[I_BEG_Y]);
  4288.    }
  4289. #endif
  4290.  
  4291.    if(upd->int_a[IA_END_IX].size < upd->int_a[IA_END_DY].size) {
  4292. #if UPD_MESSAGES & UPD_M_WARNING
  4293.       fprintf(stderr,
  4294.         "upd_open_writer: Only %d instead of %d final Xstarts\n",
  4295.         (int) upd->int_a[IA_END_IX].size,
  4296.         (int) upd->int_a[IA_END_DY].size);
  4297. #endif
  4298.       success = false;
  4299.    }
  4300.  
  4301.    if(upd->int_a[IA_ENDTOP].size < upd->int_a[IA_END_DY].size) {
  4302. #if UPD_MESSAGES & UPD_M_WARNING
  4303.       fprintf(stderr,
  4304.         "upd_open_writer: Only %d instead of %d Final Pins\n",
  4305.         (int) upd->int_a[IA_ENDTOP].size,
  4306.         (int) upd->int_a[IA_END_DY].size);
  4307. #endif
  4308.       success = false;
  4309.  
  4310.    } else {
  4311.  
  4312.       int i;
  4313.       for(i = 0; i < upd->int_a[IA_END_DY].size; ++i)
  4314.          if((upd->int_a[IA_ENDTOP].data[i] > upd->ints[I_PINS2WRITE]) ||
  4315.             (upd->int_a[IA_ENDTOP].data[i] < 0                      )   ) break;
  4316.  
  4317.       if(i < upd->int_a[IA_END_DY].size) {
  4318. #if UPD_MESSAGES & UPD_M_WARNING
  4319.          fprintf(stderr,
  4320.            "upd_open_writer: Only %d is invalid initial Pins\n",
  4321.            upd->int_a[IA_ENDTOP].data[i]);
  4322. #endif
  4323.          success = false;
  4324.       }
  4325.    }
  4326.  
  4327. /** SA_SETCOMP must be valid, if present */
  4328.    if((0 < upd->string_a[SA_SETCOMP].size) &&
  4329.       (upd->ocomp > upd->string_a[SA_SETCOMP].size)) {
  4330. #if UPD_MESSAGES & UPD_M_WARNING
  4331.       fprintf(stderr,
  4332.          "upd_open_writer: Only %d SETCOMP-Commands (%d required)\n",
  4333.          (int) upd->string_a[SA_SETCOMP].size,upd->ocomp);
  4334. #endif
  4335.       success = false;
  4336.    }
  4337.  
  4338. /** Determine required number of scan-Buffers */
  4339.  
  4340.    if(success) { /* Compute nscnbuf */
  4341.       int32 want,use;
  4342.  
  4343.       want  = upd->ints[I_NYPASS];
  4344.       want *= upd->ints[I_PINS2WRITE];
  4345.  
  4346.       if(upd->ints[I_NSCNBUF] > want) want = upd->ints[I_NSCNBUF];
  4347.  
  4348.       if(1 > want)                         want = 1;
  4349.  
  4350.       for(use = 1; 0 < use; use <<= 1) if(use > want) break;
  4351.  
  4352.       if(use <= INT_MAX) upd->nscnbuf = upd->ints[I_NSCNBUF] = use;
  4353.       else               success      = false;
  4354.  
  4355.    }                /* Compute nscnbuf */
  4356.  
  4357. /** Determine number of words in scan-buffers */
  4358.  
  4359.    if(success) { /* Compute pwidth, scnmsk, nbytes, pheight */
  4360.  
  4361.       if(0 < upd->ints[I_PWIDTH]) upd->pwidth = upd->ints[I_PWIDTH];
  4362.       else                        upd->pwidth = upd->gswidth;
  4363.  
  4364.       upd->nbytes  = (upd->pwidth+CHAR_BIT*sizeof(upd->scnbuf[0]->bytes[0]) - 1)
  4365.             /                   (CHAR_BIT*sizeof(upd->scnbuf[0]->bytes[0]));
  4366.  
  4367.       upd->scnmsk  = upd->nscnbuf - 1;
  4368.  
  4369.       if(0 < upd->ints[I_PHEIGHT]) upd->pheight = upd->ints[I_PHEIGHT];
  4370.       else                         upd->pheight = upd->gsheight;
  4371.  
  4372.    }             /* Compute pwidth, scnmsk, nbytes */
  4373.  
  4374. /** Call the writer-specific open-function */
  4375.  
  4376.    if(success) { /* Determine sizes */
  4377.       switch(upd->choice[C_FORMAT]) {
  4378.          case FMT_RAS:
  4379.             if(0 > upd_open_rascomp(udev)) success = false;
  4380.          break;
  4381.          case FMT_EPSON:
  4382.             if(0 > upd_open_wrtescp(udev)) success = false;
  4383.          break;
  4384.          case FMT_ESCP2Y:
  4385.          case FMT_ESCP2XY:
  4386.             if(0 > upd_open_wrtescp2(udev)) success = false;
  4387.          break;
  4388.          case FMT_RTL:
  4389.             if(0 > upd_open_wrtrtl(udev))   success = false;
  4390.          break;
  4391.          case FMT_CANON: /* (hr) */
  4392.             if(0 > upd_open_wrtcanon(udev)) success = false;
  4393.          break;
  4394.          default:
  4395.             success = false;
  4396. #if UPD_MESSAGES & UPD_M_WARNING
  4397.             fprintf(stderr,"upd_open_writer: Unknown writer-type %d\n",
  4398.                 upd->choice[C_FORMAT]);
  4399. #endif
  4400.          break;
  4401.       }
  4402.    }             /* Determine sizes*/
  4403.  
  4404. /** Allocate the Outputbuffer */
  4405.    if(success && (0 < upd->noutbuf)) { /* Allocate outbuf */
  4406.       upd->outbuf = gs_malloc(upd->noutbuf,sizeof(upd->outbuf[0]),"upd/outbuf");
  4407.       if(!upd->outbuf) success = false;
  4408.    }                                   /* Allocate outbuf */
  4409.  
  4410. /** Allocate the desired scan-buffer-pointers */
  4411.    if(success) {
  4412.       upd->scnbuf = gs_malloc(upd->nscnbuf,sizeof(upd->scnbuf[0]),"upd/scnbuf");
  4413.       if(NULL == upd->scnbuf) {
  4414.          success = false;
  4415.       } else {
  4416.          int ibuf;
  4417.          for(ibuf = 0; ibuf < upd->nscnbuf; ++ibuf) {
  4418.             if(success) upd->scnbuf[ibuf] =
  4419.                gs_malloc(upd->ocomp,sizeof(upd->scnbuf[0][0]),"upd/scnbuf[]");
  4420.             else upd->scnbuf[ibuf] = NULL;
  4421.  
  4422.             if(!upd->scnbuf[ibuf]) {
  4423.                success = false;
  4424.             } else {
  4425.                int icomp;
  4426.                for(icomp = 0; icomp < upd->ocomp; ++icomp) {
  4427.                   if(success) upd->scnbuf[ibuf][icomp].bytes =
  4428.                     gs_malloc(upd->nbytes,sizeof(upd->scnbuf[0][0].bytes[0]),
  4429.                     "upd/bytes");
  4430.                   else        upd->scnbuf[ibuf][icomp].bytes = NULL;
  4431.                   if(!upd->scnbuf[ibuf][icomp].bytes) success = false;
  4432.  
  4433.                   if(0 < upd->nlimits) {
  4434.  
  4435.                      upd->scnbuf[ibuf][icomp].xbegin = gs_malloc(upd->nlimits,
  4436.                         sizeof(upd->scnbuf[0][0].xbegin[0]),"upd/xbegin");
  4437.                      if(!upd->scnbuf[ibuf][icomp].xbegin) success = false;
  4438.  
  4439.                      upd->scnbuf[ibuf][icomp].xend   = gs_malloc(upd->nlimits,
  4440.                         sizeof(upd->scnbuf[0][0].xend[0]),"upd/xend");
  4441.                      if(!upd->scnbuf[ibuf][icomp].xbegin) success = false;
  4442.  
  4443.                   } else {
  4444.  
  4445.                      upd->scnbuf[ibuf][icomp].xbegin = NULL;
  4446.                      upd->scnbuf[ibuf][icomp].xend   = NULL;
  4447.  
  4448.                   }
  4449.                }
  4450.             }
  4451.          }
  4452.       }
  4453.    }
  4454.  
  4455.    if(success) upd->flags |= B_FORMAT;
  4456.    else        upd_close_writer(udev);
  4457.  
  4458.    return success ? 1 : -1;
  4459. }
  4460.  
  4461. /* ------------------------------------------------------------------- */
  4462. /* upd_close_writer: Deinitialize rendering                            */
  4463. /* ------------------------------------------------------------------- */
  4464.  
  4465. private void
  4466. upd_close_writer(upd_device *udev)
  4467. {
  4468.    const upd_p upd = udev->upd;
  4469.  
  4470.    if(upd) {
  4471.       int ibuf,icomp;
  4472.  
  4473.       if((0 < upd->noutbuf) && upd->outbuf)
  4474.          gs_free(upd->outbuf,upd->noutbuf,sizeof(upd->outbuf[0]),"upd/outbuf");
  4475.       upd->noutbuf = 0;
  4476.       upd->outbuf  = NULL;
  4477.  
  4478.       if((0 < upd->nscnbuf) && upd->scnbuf) {
  4479.          for(ibuf = 0; upd->nscnbuf > ibuf; ++ibuf) {
  4480.  
  4481.             if(!upd->scnbuf[ibuf]) continue;
  4482.  
  4483.             for(icomp = 0; icomp < upd->ocomp; ++icomp) {
  4484.  
  4485.                if((0 < upd->nbytes) && upd->scnbuf[ibuf][icomp].bytes)
  4486.                   gs_free(upd->scnbuf[ibuf][icomp].bytes,upd->nbytes,
  4487.                      sizeof(upd->scnbuf[ibuf][icomp].words[0]),"upd/bytes");
  4488.                upd->scnbuf[ibuf][icomp].bytes = NULL;
  4489.  
  4490.                if((0 < upd->nlimits) && upd->scnbuf[ibuf][icomp].xbegin)
  4491.                   gs_free(upd->scnbuf[ibuf][icomp].xbegin,upd->nlimits,
  4492.                      sizeof(upd->scnbuf[ibuf][icomp].xbegin[0]),"upd/xbegin");
  4493.                upd->scnbuf[ibuf][icomp].xbegin = NULL;
  4494.  
  4495.                if((0 < upd->nlimits) && upd->scnbuf[ibuf][icomp].xend)
  4496.                   gs_free(upd->scnbuf[ibuf][icomp].xend,upd->nlimits,
  4497.                      sizeof(upd->scnbuf[ibuf][icomp].xend[0]),"upd/xend");
  4498.                upd->scnbuf[ibuf][icomp].xend = NULL;
  4499.             }
  4500.  
  4501.             if(icomp)
  4502.                gs_free(upd->scnbuf[ibuf],upd->ocomp,sizeof(upd->scnbuf[0][0]),
  4503.                   "upd/scnbuf[]");
  4504.             upd->scnbuf[ibuf] = NULL;
  4505.  
  4506.          }
  4507.          gs_free(upd->scnbuf,upd->nscnbuf,sizeof(upd->scnbuf[0]),"upd/scnbuf");
  4508.       }
  4509.  
  4510.  
  4511.       upd->flags &= ~B_FORMAT;
  4512.    }
  4513. }
  4514.  
  4515.  
  4516. /* ------------------------------------------------------------------- */
  4517. /* upd_limits: Establish passwise limits, after rendering              */
  4518. /* ------------------------------------------------------------------- */
  4519.  
  4520. private void
  4521. upd_limits(upd_p upd, bool check)
  4522. {
  4523.    updscan_p  scans = upd->scnbuf[upd->yscnbuf & upd->scnmsk], scan;
  4524.    int   xs,x,xe,icomp,pass;
  4525.    byte *bytes,bit;
  4526.  
  4527.    for(icomp = 0; icomp < upd->ocomp; ++icomp) {
  4528.       scan = scans + icomp;
  4529.       for(pass = 0; pass < upd->nlimits; ++pass) {
  4530.          scan->xbegin[pass] = upd->pwidth;
  4531.          scan->xend[  pass] = -1;
  4532.       }
  4533.    }
  4534.  
  4535.    if(check) { /* Really check */
  4536.       for(icomp = 0; icomp < upd->ocomp; ++icomp) { /* Check Components */
  4537.          scan  = scans + icomp;
  4538.          bytes = scan->bytes;
  4539.  
  4540.          for(xs = 0; xs < upd->nbytes  && !bytes[xs];   ++xs);
  4541.  
  4542.          if(xs < upd->nbytes) { /* Has Data */
  4543.             for(xe = upd->nbytes; xs < xe && !bytes[xe-1]; --xe);
  4544.  
  4545.             for(pass = 0; pass < upd->nlimits; ++pass) { /* limit (pass) loop */
  4546.  
  4547.                x = ((xs<<3)/upd->nlimits)*upd->nlimits + pass;
  4548.                while((x >> 3) < xs) x += upd->nlimits;
  4549.  
  4550.                bit = 0x80 >> (x & 7);
  4551.                while(x < scan->xbegin[pass]) {
  4552.                   if(bytes[x>>3] & bit) scan->xbegin[pass] = x;
  4553.                   x  += upd->nlimits;
  4554.                   bit = 0x80 >> (x & 7);
  4555.                }
  4556.  
  4557.                x = (((xe<<3)|7)/upd->nlimits)*upd->nlimits + pass;
  4558.  
  4559.                while((x >> 3) < xe) x += upd->nlimits;
  4560.                while((x >> 3) > xe) x -= upd->nlimits;
  4561.  
  4562.                bit = 0x80 >> (xs & 7);
  4563.                while(x > scan->xend[pass]) {
  4564.                   if(bytes[x>>3] & bit) scan->xend[pass] = x;
  4565.                   x -= upd->nlimits;
  4566.                   bit = 0x80 >> (x & 7);
  4567.                }
  4568.  
  4569.             }                                            /* limit (pass) loop */
  4570.  
  4571.          }                      /* Has Data */
  4572.  
  4573.       }                                             /* Check Components */
  4574.  
  4575.    }           /* Really check */
  4576.  
  4577. }
  4578.  
  4579. /* ------------------------------------------------------------------- */
  4580. /* upd_open_rascomp: ncomp * 1Bit Raster-Writer                        */
  4581. /* ------------------------------------------------------------------- */
  4582.  
  4583. private int
  4584. upd_open_rascomp(upd_device *udev)
  4585. {
  4586.    const upd_p upd = udev->upd;
  4587.    int32 noutbuf;
  4588.    int error = 0;
  4589.  
  4590.    noutbuf = upd->pwidth;
  4591.  
  4592.    if(1 < upd->ncomp) noutbuf *= 8; /* ??? upd->ocomp */
  4593.  
  4594.    noutbuf = ((noutbuf+15)>>4)<<1;
  4595.  
  4596.    if(INT_MAX >= noutbuf) {
  4597.       upd->noutbuf = noutbuf;
  4598.       upd->start_writer = upd_start_rascomp;
  4599.       upd->writer       = upd_rascomp;
  4600.    } else {
  4601.       error = -1;
  4602.    }
  4603.  
  4604.    return error;
  4605. }
  4606.  
  4607. /* ------------------------------------------------------------------- */
  4608. /* upd_start_rascomp: write appropiate raster-header                   */
  4609. /* ------------------------------------------------------------------- */
  4610. #if arch_is_big_endian
  4611. #define put32(I32,Out)       \
  4612.    fwrite(&I32,1,4,Out)
  4613. #else
  4614. #define put32(I32,Out)       \
  4615.    putc(((I32)>>24)&255,Out),\
  4616.    putc(((I32)>>16)&255,Out),\
  4617.    putc(((I32)>> 8)&255,Out),\
  4618.    putc( (I32)     &255,Out)
  4619. #endif
  4620.  
  4621. private int
  4622. upd_start_rascomp(upd_p upd, FILE *out) {
  4623.  
  4624. /** if no begin-sequence externally set */
  4625.    if(0 == upd->strings[S_BEGIN].size) {
  4626.       int32 val;
  4627.  
  4628. /**   ras_magic */
  4629.       val = 0x59a66a95;
  4630.       put32(val,out);
  4631.  
  4632. /**   ras_width */
  4633.       val = upd->pwidth;
  4634.       put32(val,out);
  4635.  
  4636. /**   ras_height */
  4637.       val = upd->pheight;
  4638.       put32(val,out);
  4639.  
  4640. /**   ras_depth */
  4641.       if(1 < upd->ncomp) val = 8; /* ??? upd->ocomp */
  4642.       else               val = 1;
  4643.       put32(val,out);
  4644.  
  4645. /**   ras_length */
  4646.       val *= upd->pwidth;
  4647.       val = ((val+15)>>4)<<1;
  4648.       val *= upd->pheight;
  4649.       put32(val,out);
  4650.  
  4651. /**   ras_type */
  4652.       val = 1;
  4653.       put32(val,out);
  4654.  
  4655. /**   ras_maptype */
  4656.       val = 1;
  4657.       put32(val,out);
  4658.  
  4659. /**   ras_maplength */
  4660.       val = 3 * (1 << upd->ncomp); /* ??? upd->ocomp */
  4661.       put32(val,out);
  4662.  
  4663. /**   R,G,B-Map */
  4664.       if(1 == upd->ncomp) { /* ??? upd->ocomp */
  4665.          const updcomp_p comp = upd->valptr[0];
  4666.  
  4667.          if(upd->cmap[comp->cmap].rise) {
  4668.             putc((char) 0x00,out); putc((char) 0xff,out);
  4669.             putc((char) 0x00,out); putc((char) 0xff,out);
  4670.             putc((char) 0x00,out); putc((char) 0xff,out);
  4671.          } else {
  4672.             putc((char) 0xff,out); putc((char) 0x00,out);
  4673.             putc((char) 0xff,out); putc((char) 0x00,out);
  4674.             putc((char) 0xff,out); putc((char) 0x00,out);
  4675.          }
  4676.  
  4677.       } else if(3 == upd->ncomp) { /* ??? upd->ocomp */
  4678.          int rgb;
  4679.  
  4680.          for( rgb = 0; rgb < 3; ++rgb) {
  4681.             int entry;
  4682.             for(entry = 0; entry < 8; ++entry) {
  4683.                byte xval = upd->cmap[rgb].rise ? 0x00 : 0xff;
  4684.                if(entry & (1<<upd->cmap[rgb].comp)) xval ^= 0xff;
  4685.                putc(xval,out);
  4686.             }
  4687.          }
  4688.       } else { /* we have 4 components */
  4689.          int rgb;
  4690.  
  4691.          for(rgb = 16; 0 <= rgb; rgb -= 8) {
  4692.             int entry;
  4693.             for(entry = 0; entry < 16; ++entry) {
  4694.                uint32 rgbval = 0;
  4695.  
  4696.                if(entry & (1<<upd->cmap[0].comp)) {
  4697.  
  4698.                   rgbval = 0xffffff;
  4699.  
  4700.                } else {
  4701.  
  4702.                   if(entry & (1<<upd->cmap[1].comp)) rgbval |= 0xff0000;
  4703.                   if(entry & (1<<upd->cmap[2].comp)) rgbval |= 0x00ff00;
  4704.                   if(entry & (1<<upd->cmap[3].comp)) rgbval |= 0x0000ff;
  4705.                }
  4706.  
  4707.                if(!upd->cmap[1].rise) rgbval ^= 0xff0000;
  4708.                if(!upd->cmap[2].rise) rgbval ^= 0x00ff00;
  4709.                if(!upd->cmap[3].rise) rgbval ^= 0x0000ff;
  4710.  
  4711.                if(!(upd->choice[C_MAPPER] == MAP_RGBW)) rgbval ^= 0xffffff;
  4712.  
  4713.                putc((rgbval>>rgb)&255,out);
  4714.             }
  4715.          }
  4716.       }
  4717.    }
  4718.    memset(upd->outbuf,0,upd->noutbuf);
  4719.  
  4720.    return 0;
  4721. }
  4722.  
  4723. /* ------------------------------------------------------------------- */
  4724. /* upd_rascomp: assemble & write a scanline                            */
  4725. /* ------------------------------------------------------------------- */
  4726. private int
  4727. upd_rascomp(upd_p upd, FILE *out) {
  4728.    updscan_p scan = upd->scnbuf[upd->yscan & upd->scnmsk];
  4729.    uint bits = upd->pwidth;
  4730.  
  4731.    if(1 == upd->ncomp) { /* ??? upd->ocomp */
  4732.       uint nbytes;
  4733.  
  4734.       nbytes = (bits+7)>>3;
  4735.       memcpy(upd->outbuf,scan->bytes,nbytes);
  4736.       if((bits &= 7)) upd->outbuf[nbytes-1] &= ((byte) 0xff) << (8-bits);
  4737.  
  4738.    } else {
  4739.  
  4740.       byte  *buf   = upd->outbuf, bit = 0x80;
  4741.       int    ibyte = 0;
  4742.  
  4743.       while(0 < bits--) {
  4744.          byte val = 0;
  4745.          switch(upd->ncomp) { /* ??? upd->ocomp */
  4746.             case 4:  if(scan[3].bytes[ibyte] & bit) val |= 8;
  4747.             case 3:  if(scan[2].bytes[ibyte] & bit) val |= 4;
  4748.                      if(scan[1].bytes[ibyte] & bit) val |= 2;
  4749.             case 1:  if(scan[0].bytes[ibyte] & bit) val |= 1;
  4750.          }
  4751.          *buf++ = val;
  4752.          if(!(bit >>= 1)) {
  4753.             bit    = 0x80;
  4754.             ibyte += 1;
  4755.          }
  4756.       }
  4757.    }
  4758.  
  4759.    fwrite(upd->outbuf,1,upd->noutbuf,out);
  4760.    upd->yscan += 1;
  4761.  
  4762.    return 0;
  4763. }
  4764.  
  4765. /* ------------------------------------------------------------------- */
  4766. /* upd_open_wrtescp: ESC/P Writer intended for ESC * m commands        */
  4767. /* ------------------------------------------------------------------- */
  4768.  
  4769. private int
  4770. upd_open_wrtescp(upd_device *udev)
  4771. {
  4772.    const upd_p      upd  = udev->upd;
  4773.    int              error = 0;
  4774.  
  4775. /** Adjust the PageLength, If Requested */
  4776.    if((B_PAGELENGTH & upd->flags) &&
  4777.       (0 < upd->strings[S_BEGIN].size)) { /* BOP-Checker */
  4778.      int   i,state = 0,value = 0;
  4779.      byte *bp = (byte *) upd->strings[S_BEGIN].data;
  4780.      for(i = 0; i < upd->strings[S_BEGIN].size; ++i) {
  4781.         switch(state) {
  4782.            case  0:
  4783.               if(0x1b == bp[i]) state = 1;
  4784.            break;
  4785.            case  1:
  4786.               if('C'  == bp[i]) state = 2;
  4787.               else              state = 0;
  4788.            break;
  4789.            case  2:
  4790.               if(bp[i]) {
  4791.                  value = 0.5 + udev->height * (float) bp[i]
  4792.                                / udev->y_pixels_per_inch;
  4793.                  if(       0 >= value) bp[i] = 1;
  4794.                  else if(128 >  value) bp[i] = value;
  4795.                  else                  bp[i] = 127;
  4796.                  state = 0;
  4797.               } else {
  4798.                  state = 3;
  4799.               }
  4800.            break;
  4801.            case  3:
  4802.               value = 0.5 + udev->height / udev->y_pixels_per_inch;
  4803.               if(       0 >= value) bp[i] = 1;
  4804.               else if( 22 >  value) bp[i] = value;
  4805.               else                  bp[i] = 22;
  4806.               state = 0;
  4807.            break;
  4808.         }
  4809.      }
  4810.    }                                    /* BOP-Checker */
  4811.  
  4812.  
  4813. /** Either SETLF or YMOVE must be set */
  4814.    if((0 == upd->strings[S_SETLF].size) &&
  4815.       (0 == upd->strings[S_YMOVE].size)   ) {
  4816. #if UPD_MESSAGES & UPD_M_WARNING
  4817.       fprintf(stderr,
  4818.         "ESC/P-Open: Either SETLF- or YMOVE-Command must be present\n");
  4819. #endif
  4820.       error = -1;
  4821.    }
  4822.  
  4823. /** X-Positioning must be set too */
  4824.    if(((1 <  upd->ints[I_XSTEP]        ) &&
  4825.        (0 == upd->strings[S_XSTEP].size)   ) ||
  4826.       ((1 < upd->ints[I_NXPASS]        ) &&
  4827.        (0 == upd->strings[S_XMOVE].size) &&
  4828.        (0 == upd->strings[S_XSTEP].size)   )   ) {
  4829. #if UPD_MESSAGES & UPD_M_WARNING
  4830.       fprintf(stderr,
  4831.          "ESC/P-Open: Missing XSTEP- and/or XMOVE-Command\n");
  4832. #endif
  4833.       error = -1;
  4834.    }
  4835.  
  4836. /** SA_WRITECOMP must be valid */
  4837.    if(upd->ncomp > upd->string_a[SA_WRITECOMP].size) { /* ??? upd->ocomp */
  4838. #if UPD_MESSAGES & UPD_M_WARNING
  4839.       fprintf(stderr,
  4840.          "ESC/P-Open: WRITECOMP-Commands must be given\n");
  4841. #endif
  4842.       error = -1;
  4843.    }
  4844.  
  4845. /**
  4846. If all this is correct, it's time to coumput the size of the output-buffer.
  4847. It must hold:
  4848.   1. Y-Positioning
  4849.   2. X-Positioning
  4850.   3. Component-Selection
  4851.   4. The Raster-Command
  4852.   5. The Data
  4853. */
  4854.    if(0 <= error) {
  4855.       int32 i,noutbuf,need;
  4856.  
  4857.       if(0 < upd->strings[S_YMOVE].size) {
  4858.          noutbuf = upd->strings[S_YMOVE].size + 2;
  4859.       } else {
  4860.          int nmax = upd->pheight;
  4861.          if(      1 < upd->ints[I_YSTEP]) nmax /=  upd->ints[I_YSTEP];
  4862.          else if(-1 > upd->ints[I_YSTEP]) nmax *= -upd->ints[I_YSTEP];
  4863.          noutbuf  = 2 * upd->strings[S_SETLF].size + 2;
  4864.          noutbuf += nmax/255 + 1;
  4865.       }
  4866.  
  4867.       if(1 < upd->ints[I_YSTEP])
  4868.          noutbuf += (upd->ints[I_YSTEP]-1) * upd->strings[S_YSTEP].size;
  4869.  
  4870.       noutbuf +=  upd->strings[S_XMOVE].size + 2;
  4871.  
  4872.       if(1 < upd->ints[I_XSTEP])
  4873.          noutbuf += (upd->ints[I_XSTEP]-1) * upd->strings[S_XSTEP].size;
  4874.  
  4875.       if(0 < upd->string_a[SA_SETCOMP].size) {
  4876.          need = 0;
  4877.          for(i = 0; i < upd->ocomp; ++i)
  4878.             if(need < upd->string_a[SA_SETCOMP].data[i].size)
  4879.                need = upd->string_a[SA_SETCOMP].data[i].size;
  4880.          noutbuf += need;
  4881.       }
  4882.  
  4883.       need = 0;
  4884.       for(i = 0; i < upd->ocomp; ++i)
  4885.          if(need < upd->string_a[SA_WRITECOMP].data[i].size)
  4886.             need = upd->string_a[SA_WRITECOMP].data[i].size;
  4887.       noutbuf += need + 2;
  4888.  
  4889.       noutbuf += ((upd->ints[I_PINS2WRITE] + 7) / 8)
  4890.                * ((upd->pwidth + upd->ints[I_NXPASS] - 1)/upd->ints[I_NXPASS]);
  4891.  
  4892.       if((0 < noutbuf) && (noutbuf <= INT_MAX)) {
  4893.          upd->noutbuf      = noutbuf;
  4894.          upd->writer       = upd_wrtescp;
  4895.          upd->nlimits      = upd->ints[I_NXPASS];
  4896.          error             = 1;
  4897.       } else {
  4898.          error = -1;
  4899. #if      UPD_MESSAGES & UPD_M_WARNING
  4900.             fprintf(stderr,
  4901.               "ESC/P-Open: %ld is unreasonable size of Outputbuffer\n",
  4902.               (long) noutbuf);
  4903. #endif
  4904.       }
  4905.    }
  4906.  
  4907.    return error;
  4908. }
  4909.  
  4910. /* ------------------------------------------------------------------- */
  4911. /* upd_wrtescp: Write a pass                                           */
  4912. /* ------------------------------------------------------------------- */
  4913.  
  4914. private int
  4915. upd_wrtescp(upd_p upd, FILE *out)
  4916. {
  4917.    int  pinbot,pin,pintop,xbegin,x,xend,icomp,ybegin,yend,y,ioutbuf,n,ixpass;
  4918.    byte *obytes,bit;
  4919.    updscan_p scan;
  4920.  
  4921. /** Determine the number of pins to write */
  4922.  
  4923.    if(upd->yscan < upd->ints[I_BEG_Y]) {
  4924.       ixpass = upd->int_a[IA_BEG_IX].data[upd->ipass];
  4925.       pintop = 0;
  4926.       pinbot = upd->int_a[IA_BEGBOT].data[upd->ipass];
  4927.    } else if(upd->yscan >= upd->ints[I_END_Y]) {
  4928.       ixpass = upd->int_a[IA_END_IX].data[upd->ipass];
  4929.       pinbot = upd->ints[I_PINS2WRITE];
  4930.       pintop = pinbot - upd->int_a[IA_ENDTOP].data[upd->ipass];
  4931.    } else {
  4932.       ixpass = upd->int_a[IA_STD_IX].data[upd->ipass];
  4933.       pintop = 0;
  4934.       pinbot = upd->ints[I_PINS2WRITE];
  4935.    }
  4936.  
  4937.    ybegin =  pintop * upd->ints[I_NYPASS] + upd->yscan - upd->ints[I_BEGSKIP];
  4938.    yend   =  pinbot * upd->ints[I_NYPASS] + upd->yscan - upd->ints[I_BEGSKIP];
  4939.  
  4940. /** Determine Width of this scan */
  4941.  
  4942.    xbegin = upd->pwidth;
  4943.    xend   = -1;
  4944.  
  4945.    for(y = ybegin; y < yend; y += upd->ints[I_NYPASS]) { /* Pin-testloop */
  4946.  
  4947.       if(0 > y) continue; /* Inserted Scanlines */
  4948.  
  4949.       scan = upd->scnbuf[y & upd->scnmsk];
  4950.  
  4951.       for(icomp = 0; icomp < upd->ocomp; ++icomp) { /* Compwise test */
  4952.          if(xbegin > scan[icomp].xbegin[ixpass])
  4953.             xbegin = scan[icomp].xbegin[ixpass];
  4954.          if(xend   < scan[icomp].xend[  ixpass])
  4955.             xend   = scan[icomp].xend[  ixpass];
  4956.       }                                             /* Compwise test */
  4957.  
  4958.    }                                                     /* Pin-testloop */
  4959.  
  4960.    if(xbegin <= xend) { /* Some data to write */
  4961.  
  4962.       ioutbuf = 0;
  4963.  
  4964.       if(0 == upd->strings[S_XMOVE].size) xbegin = ixpass;
  4965.  
  4966. /*
  4967.  *    Adjust the Printers Y-Position
  4968.  */
  4969.       if(upd->yscan != upd->yprinter) { /* Adjust Y-Position */
  4970.          if(B_YABS & upd->flags) y = upd->yscan + upd->ints[I_YOFS];
  4971.          else                    y = upd->yscan - upd->yprinter;
  4972.  
  4973.          if(      1 < upd->ints[I_YSTEP]) {
  4974.             n      =  y / upd->ints[I_YSTEP];  /* Major-Steps */
  4975.             y     -=  n * upd->ints[I_YSTEP];  /* Minor-Steps */
  4976.          } else if(-1 > upd->ints[I_YSTEP]) {
  4977.             n      = y * -upd->ints[I_YSTEP];  /* May this work? */
  4978.             y      = 0;
  4979.          } else {
  4980.             n      = y;
  4981.             y      = 0;
  4982.          }
  4983.  
  4984.          if(n) { /* Coarse Positioning */
  4985.             if(0 < upd->strings[S_YMOVE].size) {
  4986.  
  4987.                memcpy(upd->outbuf+ioutbuf,
  4988.                           upd->strings[S_YMOVE].data,
  4989.                           upd->strings[S_YMOVE].size);
  4990.                ioutbuf += upd->strings[S_YMOVE].size;
  4991.  
  4992.                upd->outbuf[ioutbuf++] =  n     & 0xff;
  4993.                upd->outbuf[ioutbuf++] = (n>>8) & 0xff;
  4994.  
  4995.             } else {
  4996.  
  4997.                while(n) {
  4998.                   int n2do = n > 255 ? 255 : n;
  4999.                   if(upd->lf != n2do) {
  5000.                      memcpy(upd->outbuf+ioutbuf,
  5001.                                 upd->strings[S_SETLF].data,
  5002.                                 upd->strings[S_SETLF].size);
  5003.                      ioutbuf += upd->strings[S_SETLF].size;
  5004.                      upd->outbuf[ioutbuf++] = n2do;
  5005.                      upd->lf                = n2do;
  5006.                   }
  5007.                   upd->outbuf[ioutbuf++] = '\n';
  5008.                   n -= n2do;
  5009.                }
  5010.             }
  5011.          }       /* Coarse Positioning */
  5012.  
  5013.          if(0 < upd->strings[S_YSTEP].size) {
  5014.             while(y--) {
  5015.                memcpy(upd->outbuf+ioutbuf,
  5016.                           upd->strings[S_YSTEP].data,
  5017.                           upd->strings[S_YSTEP].size);
  5018.                ioutbuf += upd->strings[S_YSTEP].size;
  5019.             }
  5020.          }
  5021.  
  5022.          upd->yprinter = upd->yscan;
  5023.       }                                 /* Adjust Y-Position */
  5024.  
  5025. /*
  5026.  * Now write the required components
  5027.  */
  5028.       for(icomp = 0; icomp < upd->ocomp; ++icomp) { /* Component-Print */
  5029. /*
  5030.  *       First check, wether this Component needs printing
  5031.  */
  5032.          for(y = ybegin; y < yend; y += upd->ints[I_NYPASS]) { /* Comp-Test */
  5033.             if(0 > y) continue;
  5034.             scan = upd->scnbuf[y & upd->scnmsk]+icomp;
  5035.             if(0 <= scan->xend[ixpass]) break;
  5036.          }                                                     /* Comp-Test */
  5037.          if(y >= yend) continue; /* Component not required */
  5038. /*
  5039.  *       Select the Component
  5040.  */
  5041.          if((0 < upd->string_a[SA_SETCOMP].size) &&
  5042.             (upd->icomp != icomp               )   ) { /* Selection enabled */
  5043.             upd->icomp = icomp;
  5044.             if(0 < upd->string_a[SA_SETCOMP].data[icomp].size) {
  5045.                memcpy(upd->outbuf+ioutbuf,
  5046.                           upd->string_a[SA_SETCOMP].data[icomp].data,
  5047.                           upd->string_a[SA_SETCOMP].data[icomp].size);
  5048.                ioutbuf += upd->string_a[SA_SETCOMP].data[icomp].size;
  5049.             }
  5050.          }                                      /* Selection enabled */
  5051. /*
  5052.  *       Establish the X-Position
  5053.  */
  5054.          if(xbegin != upd->xprinter) {
  5055.  
  5056.             if(0 == upd->strings[S_XMOVE].size) {
  5057.  
  5058.                upd->outbuf[ioutbuf++] = '\r';
  5059.                upd->xprinter          =  0;
  5060.                n = 0;
  5061.                x = ixpass;
  5062.  
  5063.             } else {
  5064.  
  5065.                if(B_XABS & upd->flags) n = x = xbegin + upd->ints[I_XOFS];
  5066.                else                    n = x = xbegin - upd->xprinter;
  5067.  
  5068.                if(        1 < upd->ints[I_XSTEP]) {
  5069.                   if(0 > n) {
  5070.                      n  -= upd->ints[I_XSTEP];
  5071.                      x  -= n;
  5072.                   }
  5073.                   if(n) n  /= upd->ints[I_XSTEP]; /* Major-Steps */
  5074.                   if(x) x  %= upd->ints[I_XSTEP]; /* Minor-Steps */
  5075.  
  5076.                } else if(-1 > upd->ints[I_XSTEP]) {
  5077.                   n *= -upd->ints[I_XSTEP]; /* May this work? */
  5078.                   x  = 0;
  5079.                }
  5080.  
  5081.                if(n) { /* Adjust X-Position */
  5082.  
  5083.                  memcpy(upd->outbuf+ioutbuf,
  5084.                              upd->strings[S_XMOVE].data,
  5085.                              upd->strings[S_XMOVE].size);
  5086.                   ioutbuf += upd->strings[S_XMOVE].size;
  5087.  
  5088.                   upd->outbuf[ioutbuf++] =  n     & 0xff;
  5089.                   upd->outbuf[ioutbuf++] = (n>>8) & 0xff;
  5090.  
  5091.                }       /* Adjust X-Position */
  5092.  
  5093.             }
  5094.  
  5095.             if(x && 0 < upd->strings[S_XSTEP].size) { /* Fine-Adjust X */
  5096.                while(x--) {
  5097.                   memcpy(upd->outbuf+ioutbuf,
  5098.                              upd->strings[S_XSTEP].data,
  5099.                              upd->strings[S_XSTEP].size);
  5100.                   ioutbuf += upd->strings[S_XSTEP].size;
  5101.                }
  5102.             }                                         /* Fine-Adjust X */
  5103.          }
  5104.          upd->xprinter = xend+1;
  5105. /*
  5106.  *       Send the Write-Command
  5107.  */
  5108.          if(0 < upd->string_a[SA_WRITECOMP].data[icomp].size) {
  5109.             memcpy(upd->outbuf+ioutbuf,
  5110.                        upd->string_a[SA_WRITECOMP].data[icomp].data,
  5111.                        upd->string_a[SA_WRITECOMP].data[icomp].size);
  5112.             ioutbuf += upd->string_a[SA_WRITECOMP].data[icomp].size;
  5113.          }
  5114.          n = (xend - xbegin) / upd->ints[I_NXPASS] + 1;;
  5115.          upd->outbuf[ioutbuf++] =  n     & 255;
  5116.          upd->outbuf[ioutbuf++] = (n>>8) & 255;
  5117. /*
  5118.  *       Clear the data-Part
  5119.  */
  5120.          obytes   =  upd->outbuf+ioutbuf;
  5121.          n       *= (upd->ints[I_PINS2WRITE]+7)>>3;
  5122.          memset(obytes,0,n);
  5123.          ioutbuf += n;
  5124. /*
  5125.  *       Set the Pixels
  5126.  */
  5127.          for(x = xbegin; x <= xend; x += upd->ints[I_NXPASS]) {
  5128.  
  5129.             bit     = 0x80 >> (pintop & 7);
  5130.             obytes += pintop>>3;
  5131.  
  5132.             for(pin = pintop, y = ybegin; pin < pinbot;
  5133.                 pin++,        y += upd->ints[I_NYPASS]) {
  5134.                if(0 <= y) {
  5135.                   scan = upd->scnbuf[y & upd->scnmsk]+icomp;
  5136.                   if(scan->bytes[x>>3] & (0x80 >> (x & 7))) *obytes |= bit;
  5137.                }
  5138.                if(!(bit >>= 1)) { obytes++; bit = 0x80; }
  5139.             }
  5140.  
  5141.             obytes += (upd->ints[I_PINS2WRITE]-pinbot+7)>>3;
  5142.          }
  5143. /*
  5144.  *       Send this Component to the Printer
  5145.  */
  5146.          fwrite(upd->outbuf,1,ioutbuf,out);
  5147.          ioutbuf = 0;
  5148.       }                                             /* Component-Print */
  5149.    }                    /* Some data to write */
  5150.  
  5151. /** Advance counters in upd, change modi */
  5152.  
  5153.    if(upd->yscan < upd->ints[I_BEG_Y]) {
  5154.       upd->yscan += upd->int_a[IA_BEG_DY].data[upd->ipass++];
  5155.       if(     upd->ints[I_BEG_Y] <= upd->yscan) upd->ipass = 0;
  5156.       else if(upd->int_a[IA_BEG_DY].size <= upd->ipass) upd->ipass = 0;
  5157.    } else if(upd->yscan >= upd->ints[I_END_Y]) {
  5158.       upd->yscan += upd->int_a[IA_END_DY].data[upd->ipass++];
  5159.       if(upd->int_a[IA_END_DY].size <= upd->ipass) upd->ipass = 0;
  5160.    } else {
  5161.       upd->yscan += upd->int_a[IA_STD_DY].data[upd->ipass++];
  5162.       if(upd->int_a[IA_STD_DY].size <= upd->ipass) upd->ipass = 0;
  5163.       if(upd->yscan >= upd->ints[I_END_Y])         upd->ipass = 0;
  5164.    }
  5165.  
  5166.    return 0;
  5167. }
  5168.  
  5169. /* ------------------------------------------------------------------- */
  5170. /* upd_open_wrtescp2: ESC/P2 Writer intended for ESC . 1  commands     */
  5171. /* ------------------------------------------------------------------- */
  5172.  
  5173. private int
  5174. upd_open_wrtescp2(upd_device *udev)
  5175. {
  5176.    const upd_p      upd             = udev->upd;
  5177.    int              error           = 0;
  5178.    float            pixels_per_inch = 360.0;
  5179.  
  5180. /** Analyze (and optionally adjust) the BOP-Sequence */
  5181.    if(0 < upd->strings[S_BEGIN].size) { /* BOP-Checker */
  5182.      int   i,state = 0,value = 0;
  5183.      byte *bp = (byte *) upd->strings[S_BEGIN].data;
  5184.      for(i = 0; i < upd->strings[S_BEGIN].size; ++i) {
  5185.         switch(state) {
  5186.            case  0:
  5187.               if(0x1b == bp[i]) state = 1;
  5188.            break;
  5189.            case  1:
  5190.               if('('  == bp[i]) state = 2;
  5191.               else              state = 0;
  5192.            break;
  5193.            case  2:
  5194.               switch(bp[i]) {
  5195.                  case 'U': state =  3; break; /* Printer-Resolution */
  5196.                  case 'C': state =  6; break; /* Page-Length */
  5197.                  case 'c': state = 10; break; /* Top/Bottom Margin */
  5198.                  default:  state =  0; break;
  5199.               }
  5200.            break;
  5201.            case  3:
  5202.               if(1 == bp[i]) state = 4;
  5203.               else           state = 0;
  5204.            break;
  5205.            case  4:
  5206.               if(0 == bp[i]) state = 5;
  5207.               else           state = 0;
  5208.            break;
  5209.            case  5:
  5210.               pixels_per_inch = 3600.0 / (float) bp[i];
  5211.               state = 0;
  5212.            break;
  5213.            case  6:
  5214.               if(2 == bp[i]) state = 7;
  5215.               else           state = 0;
  5216.            break;
  5217.            case  7:
  5218.               if(0 == bp[i]) state = 8;
  5219.               else           state = 0;
  5220.            break;
  5221.            case  8:
  5222.               if(B_PAGELENGTH & upd->flags) {
  5223.                  value = 0.5 + udev->height
  5224.                                * pixels_per_inch / udev->y_pixels_per_inch;
  5225.                  bp[i] =  value     & 0xff;
  5226.               }
  5227.               state = 9;
  5228.            break;
  5229.            case  9:
  5230.               if(B_PAGELENGTH & upd->flags) {
  5231.                  bp[i] = (value>>8) & 0xff;
  5232.               }
  5233.               state = 0;
  5234.            break;
  5235.            case 10:
  5236.               if(4 == bp[i]) state = 11;
  5237.               else           state =  0;
  5238.            break;
  5239.            case 11:
  5240.               if(0 == bp[i]) state = 12;
  5241.               else           state =  0;
  5242.            break;
  5243.            case  12:
  5244.               if(B_TOPMARGIN & upd->flags) {
  5245.                  value =  dev_t_margin(udev) * pixels_per_inch;
  5246.                  bp[i] =  value     & 0xff;
  5247.               }
  5248.               state = 13;
  5249.            break;
  5250.            case  13:
  5251.               if(B_TOPMARGIN & upd->flags) {
  5252.                  bp[i] = (value>>8) & 0xff;
  5253.               }
  5254.               state = 14;
  5255.            break;
  5256.            case  14:
  5257.               if(B_BOTTOMMARGIN & upd->flags) {
  5258.                  value = 0.5 + udev->height
  5259.                                * pixels_per_inch / udev->y_pixels_per_inch
  5260.                        - dev_b_margin(udev) * pixels_per_inch;
  5261.                  bp[i] =  value     & 0xff;
  5262.               }
  5263.               state = 15;
  5264.            break;
  5265.            case  15:
  5266.               if(B_BOTTOMMARGIN & upd->flags) {
  5267.                  bp[i] = (value>>8) & 0xff;
  5268.               }
  5269.               state =  0;
  5270.            break;
  5271.         }
  5272.      }
  5273.    }                                    /* BOP-Checker */
  5274.  
  5275. /** Create Y-Move-Command, if not given */
  5276.    if(0 == upd->strings[S_YMOVE].size) {
  5277.       byte *bp;
  5278.       UPD_MM_DEL_PARAM(upd->strings[S_YMOVE]);
  5279.       UPD_MM_GET_ARRAY(bp,5);
  5280.       upd->strings[S_YMOVE].data = bp;
  5281.       upd->strings[S_YMOVE].size = 5;
  5282.       *bp++ = 0x1b; /* ESC */
  5283.       *bp++ = '(';
  5284.       *bp++ = upd->flags & B_YABS ? 'V' : 'v';
  5285.       *bp++ =  2;
  5286.       *bp++ =  0;
  5287.    }
  5288.  
  5289. /** X-Positioning must be set too, sometimes */
  5290.    if((1 < upd->ints[I_XSTEP]) && (0 == upd->strings[S_XSTEP].size)) {
  5291.  
  5292. #if UPD_MESSAGES & UPD_M_WARNING
  5293.       fprintf(stderr,
  5294.          "ESC/P2-Open: XSTEP-Command required for XSTEP=%d\n",
  5295.          upd->ints[I_XSTEP]);
  5296. #endif
  5297.       error = -1;
  5298.  
  5299.    } else if((1 <  upd->ints[I_NXPASS]       ) &&
  5300.              (0 == upd->strings[S_XMOVE].size) &&
  5301.              (0 == upd->strings[S_XSTEP].size)   ) {
  5302.       byte *bp;
  5303.       int ratio;
  5304.  
  5305.       ratio = (udev->y_pixels_per_inch + .5) / udev->x_pixels_per_inch;
  5306.  
  5307.       if(0 == upd->ints[I_XSTEP]) { /* Adjust scale-factor too! */
  5308.          if(ratio > 1) upd->ints[I_XSTEP] = -ratio;
  5309.       } else {                     /* Adjust scale-factor too! */
  5310.          ratio = -upd->ints[I_XSTEP];
  5311.       }
  5312.  
  5313.       if(2 == upd->ints[I_NXPASS]) { /* Use a relative Step */
  5314.  
  5315.          UPD_MM_DEL_PARAM(upd->strings[S_XSTEP]);
  5316.          UPD_MM_GET_ARRAY(bp,4);
  5317.          upd->strings[S_XSTEP].size = 4;
  5318.          upd->strings[S_XSTEP].data = bp;
  5319.          *bp++ = 0x1b;
  5320.          *bp++ = '\\';
  5321.          *bp++ =  ratio     & 0xff;
  5322.          *bp++ = (ratio>>8) & 0xff;
  5323.  
  5324.       } else {                      /* Use relative or absolute Move */
  5325.  
  5326.          UPD_MM_DEL_PARAM(upd->strings[S_XMOVE]);
  5327.          UPD_MM_GET_ARRAY(bp,2);
  5328.          upd->strings[S_XMOVE].size = 2;
  5329.          upd->strings[S_XMOVE].data = bp;
  5330.          *bp++  = 0x1b;
  5331.          *bp++  = upd->flags & B_XABS ? '$' : '\\';
  5332.  
  5333.       }
  5334.    }
  5335.  
  5336. /** If there is neither a writecomp nor a setcomp-command, generate both */
  5337.    if((0 == upd->string_a[SA_WRITECOMP].size) &&
  5338.       (0 == upd->string_a[SA_SETCOMP].size  )   ) { /* Default-commands */
  5339.       byte *bp;
  5340.       gs_param_string *ap;
  5341.       int   i;
  5342.  
  5343.       if(4 == upd->ocomp) { /* Establish Component-Selection */
  5344.          UPD_MM_DEL_APARAM(upd->string_a[SA_SETCOMP]);
  5345.          UPD_MM_GET_ARRAY(ap,4);
  5346.          upd->string_a[SA_SETCOMP].data = ap;
  5347.          upd->string_a[SA_SETCOMP].size = 4;
  5348.          for(i = 0; i < 4; ++i) {
  5349.             UPD_MM_GET_ARRAY(bp,3);
  5350.             ap[i].size = 3;
  5351.             ap[i].data = bp;
  5352.             *bp++ = 0x1b;
  5353.             *bp++ = 'r';
  5354.             switch(((updcomp_p)upd->valptr[i])->cmap) { /* use COMPORDER! */
  5355.                case 0: *bp++ = 0; break; /* Black */
  5356.                case 1: *bp++ = 2; break; /* Cyan */
  5357.                case 2: *bp++ = 1; break; /* Magenta */
  5358.                case 3: *bp++ = 4; break; /* Yellow */
  5359.             }                                           /* use COMPORDER! */
  5360.          }
  5361.       }                     /* Establish Component-Selection */
  5362.  
  5363.       UPD_MM_DEL_APARAM(upd->string_a[SA_WRITECOMP]);
  5364.       UPD_MM_GET_ARRAY(ap,upd->ocomp);
  5365.       upd->string_a[SA_WRITECOMP].data = ap;
  5366.       upd->string_a[SA_WRITECOMP].size = upd->ncomp;
  5367.       for(i = 0; i < upd->ocomp; ++i) {
  5368.          UPD_MM_GET_ARRAY(bp,6);
  5369.          ap[i].size = 6;
  5370.          ap[i].data = bp;
  5371.          *bp++ = 0x1b;
  5372.          *bp++ = '.';
  5373.          *bp++ =  1;  /* RLE */
  5374.          *bp++ = 3600.0 * upd->ints[I_NYPASS] / udev->y_pixels_per_inch + 0.5;
  5375.          *bp++ = 3600.0 * upd->ints[I_NXPASS] / udev->x_pixels_per_inch + 0.5;
  5376.          *bp++ = upd->ints[I_PINS2WRITE];
  5377.       }
  5378.    }                                                /* Default-commands */
  5379.  
  5380. /** SA_WRITECOMP must be valid */
  5381.    if(upd->ocomp > upd->string_a[SA_WRITECOMP].size) {
  5382. #if UPD_MESSAGES & UPD_M_WARNING
  5383.       fprintf(stderr,
  5384.          "ESC/P2-Open: WRITECOMP-Commands must be given\n");
  5385. #endif
  5386.       error = -1;
  5387.    }
  5388.  
  5389. /** Check Validity of X-Pass */
  5390.    switch(upd->choice[C_FORMAT]) {
  5391.       case FMT_ESCP2Y:
  5392.          if(1 < upd->ints[I_NXPASS]) {
  5393. #if         UPD_MESSAGES & UPD_M_WARNING
  5394.                fprintf(stderr,
  5395.                   "ESC/P2-Open: FMT_ESCP2Y cannot handle multiple X-Passes\n");
  5396. #endif
  5397.             error = -1;
  5398.          } else {
  5399.             upd->writer = upd_wrtescp2;
  5400.          }
  5401.       break;
  5402.       case FMT_ESCP2XY:
  5403.          upd->writer  = upd_wrtescp2x;
  5404.          upd->nlimits = upd->ints[I_NXPASS];
  5405. #if      UPD_MESSAGES & UPD_M_WARNING
  5406.             if(1 == upd->ints[I_NXPASS])
  5407.                fprintf(stderr,
  5408.                   "ESC/P2-Open: FMT_ESCP2XY should not be used with 1X-Pass\n");
  5409. #endif
  5410.       break;
  5411.       default:
  5412. #if      UPD_MESSAGES & UPD_M_WARNING
  5413.             fprintf(stderr,
  5414.                "ESC/P2-Open: %d is not a ESC/P2-Format\n",
  5415.                upd->choice[C_FORMAT]);
  5416. #endif
  5417.          error = - 1;
  5418.       break;
  5419.    }
  5420.  
  5421.  
  5422. /**
  5423. If all this is correct, it's time to compute the size of the output-buffer.
  5424. It must hold:
  5425.   1. Y-Positioning
  5426.   2. X-Positioning
  5427.   3. Component-Selection
  5428.   4. The Raster-Command
  5429.   5. The Data
  5430. */
  5431.    if(0 <= error) {
  5432.       int32 i,noutbuf,need;
  5433.  
  5434.       if(0 < upd->strings[S_YMOVE].size) {
  5435.          noutbuf = upd->strings[S_YMOVE].size + 2;
  5436.       } else {
  5437.          int nmax = upd->pheight;
  5438.          if(      1 < upd->ints[I_YSTEP]) nmax /=  upd->ints[I_YSTEP];
  5439.          else if(-1 > upd->ints[I_YSTEP]) nmax *= -upd->ints[I_YSTEP];
  5440.          noutbuf  = 2 * upd->strings[S_SETLF].size + 2;
  5441.          noutbuf += nmax/255 + 1;
  5442.       }
  5443.  
  5444.       if(1 < upd->ints[I_YSTEP])
  5445.          noutbuf += (upd->ints[I_YSTEP]-1) * upd->strings[S_YSTEP].size;
  5446.  
  5447.       if(0 == upd->strings[S_XMOVE].size) {
  5448.          noutbuf += 1; /* The CR */
  5449.          noutbuf += (upd->ints[I_NXPASS]-1) * upd->strings[S_XSTEP].size;
  5450.       } else {
  5451.          noutbuf +=  upd->strings[S_XMOVE].size + 2;
  5452.  
  5453.          if(1 < upd->ints[I_XSTEP])
  5454.             noutbuf += (upd->ints[I_XSTEP]-1) * upd->strings[S_XSTEP].size;
  5455.       }
  5456.  
  5457.       if(0 < upd->string_a[SA_SETCOMP].size) {
  5458.          need = 0;
  5459.          for(i = 0; i < upd->ocomp; ++i)
  5460.             if(need < upd->string_a[SA_SETCOMP].data[i].size)
  5461.                need = upd->string_a[SA_SETCOMP].data[i].size;
  5462.          noutbuf += need;
  5463.       }
  5464.  
  5465.       need = 0;
  5466.       for(i = 0; i < upd->ocomp; ++i)
  5467.          if(need < upd->string_a[SA_WRITECOMP].data[i].size)
  5468.             need = upd->string_a[SA_WRITECOMP].data[i].size;
  5469.       noutbuf += need + 2;
  5470.  
  5471.       noutbuf += 2*upd->nbytes + (upd->nbytes + 127) / 128;
  5472.  
  5473.       upd->noutbuf      = noutbuf;
  5474.       error             = 1;
  5475.  
  5476.    }
  5477.  
  5478.    return error;
  5479. }
  5480.  
  5481. /* ------------------------------------------------------------------- */
  5482. /* upd_wrtescp2: Write a pass                                          */
  5483. /* ------------------------------------------------------------------- */
  5484.  
  5485. private int
  5486. upd_wrtescp2(upd_p upd, FILE *out)
  5487. {
  5488.    int  pinbot,pin,pintop,xbegin,x,xend,icomp,ybegin,yend,y,ioutbuf,n;
  5489.    byte *obytes;
  5490.    updscan_p scan;
  5491.  
  5492. /** Determine the number of pins to write */
  5493.  
  5494.    if(upd->yscan < upd->ints[I_BEG_Y]) {
  5495.       pintop = 0;
  5496.       pinbot = upd->int_a[IA_BEGBOT].data[upd->ipass];
  5497.    } else if(upd->yscan >= upd->ints[I_END_Y]) {
  5498.       pinbot = upd->ints[I_PINS2WRITE];
  5499.       pintop = pinbot - upd->int_a[IA_ENDTOP].data[upd->ipass];
  5500.    } else {
  5501.       pintop = 0;
  5502.       pinbot = upd->ints[I_PINS2WRITE];
  5503.    }
  5504.  
  5505.    ybegin =  pintop * upd->ints[I_NYPASS] + upd->yscan - upd->ints[I_BEGSKIP];
  5506.    yend   =  pinbot * upd->ints[I_NYPASS] + upd->yscan - upd->ints[I_BEGSKIP];
  5507.  
  5508. /** Determine Width of this scan */
  5509.  
  5510.    xbegin = upd->nbytes;
  5511.    xend   = -1;
  5512.  
  5513.    for(y = ybegin; y < yend; y += upd->ints[I_NYPASS]) { /* Pin-testloop */
  5514.  
  5515.       if(0 > y) continue; /* Inserted Scanlines */
  5516.  
  5517.       scan = upd->scnbuf[y & upd->scnmsk];
  5518.  
  5519.       for(icomp = 0; icomp < upd->ocomp; ++icomp) { /* Compwise test */
  5520.          obytes = scan[icomp].bytes;
  5521.  
  5522.          for(x = 0; x < xbegin && !obytes[x]; x++);
  5523.          if(x < xbegin) xbegin = x;
  5524.  
  5525.          if(x < upd->nbytes) {
  5526.             for(x = upd->nbytes-1; x > xend && !obytes[x]; x--);
  5527.             if(x > xend) xend = x;
  5528.          }
  5529.       }                                             /* Compwise test */
  5530.  
  5531.    }                                                     /* Pin-testloop */
  5532.  
  5533.    if(xbegin <= xend) { /* Some data to write */
  5534.  
  5535.       ioutbuf = 0;
  5536.  
  5537.       if(0 == upd->strings[S_XMOVE].size) xbegin = 0;
  5538.  
  5539. /*
  5540.  *    Adjust the Printers Y-Position
  5541.  */
  5542.       if(upd->yscan != upd->yprinter) { /* Adjust Y-Position */
  5543.          if(B_YABS & upd->flags) y = upd->yscan + upd->ints[I_YOFS];
  5544.          else                    y = upd->yscan - upd->yprinter;
  5545.  
  5546.          if(      1 < upd->ints[I_YSTEP]) {
  5547.             n      =  y / upd->ints[I_YSTEP];  /* Major-Steps */
  5548.             y     -=  n * upd->ints[I_YSTEP];  /* Minor-Steps */
  5549.          } else if(-1 > upd->ints[I_YSTEP]) {
  5550.             n      = y * -upd->ints[I_YSTEP];  /* May this work? */
  5551.             y      = 0;
  5552.          } else {
  5553.             n      = y;
  5554.             y      = 0;
  5555.          }
  5556.  
  5557.          if(n) { /* Coarse Positioning */
  5558.             memcpy(upd->outbuf+ioutbuf,
  5559.                        upd->strings[S_YMOVE].data,upd->strings[S_YMOVE].size);
  5560.             ioutbuf += upd->strings[S_YMOVE].size;
  5561.  
  5562.             upd->outbuf[ioutbuf++] =  n     & 0xff;
  5563.             upd->outbuf[ioutbuf++] = (n>>8) & 0xff;
  5564.  
  5565.          }       /* Coarse Positioning */
  5566.  
  5567.          if(0 < upd->strings[S_YSTEP].size) {
  5568.             while(y--) {
  5569.                memcpy(upd->outbuf+ioutbuf,
  5570.                           upd->strings[S_YSTEP].data,
  5571.                           upd->strings[S_YSTEP].size);
  5572.                ioutbuf += upd->strings[S_YSTEP].size;
  5573.             }
  5574.          }
  5575.  
  5576.          upd->yprinter = upd->yscan;
  5577.       }                                 /* Adjust Y-Position */
  5578. /*
  5579.  * Now write the required components
  5580.  */
  5581.       for(icomp = 0; icomp < upd->ocomp; ++icomp) { /* Component-Print */
  5582. /*
  5583.  *       First check, wether this Component needs printing
  5584.  */
  5585.          for(y = ybegin; y < yend; y += upd->ints[I_NYPASS]) { /* Comp-Test */
  5586.             if(0 > y) continue;
  5587.             obytes = upd->scnbuf[y & upd->scnmsk][icomp].bytes;
  5588.             for(x = xbegin; x <= xend && !obytes[x]; ++x);
  5589.             if(             x <= xend) break;
  5590.          }                                                     /* Comp-Test */
  5591.          if(y >= yend) continue; /* Component not required */
  5592. /*
  5593.  *       Select the Component
  5594.  */
  5595.          if((0 < upd->string_a[SA_SETCOMP].size) &&
  5596.             (upd->icomp != icomp               )   ) { /* Selection enabled */
  5597.             upd->icomp = icomp;
  5598.             if(0 < upd->string_a[SA_SETCOMP].data[icomp].size) {
  5599.                memcpy(upd->outbuf+ioutbuf,
  5600.                           upd->string_a[SA_SETCOMP].data[icomp].data,
  5601.                           upd->string_a[SA_SETCOMP].data[icomp].size);
  5602.                ioutbuf += upd->string_a[SA_SETCOMP].data[icomp].size;
  5603.             }
  5604.          }                                      /* Selection enabled */
  5605. /*
  5606.  *       Establish the X-Position
  5607.  */
  5608.          if(xbegin != upd->xprinter) {
  5609.  
  5610.             if(0 == upd->strings[S_XMOVE].size) {
  5611.  
  5612.                upd->outbuf[ioutbuf++] = '\r';
  5613.                upd->xprinter          =  0;
  5614.                n = 0;
  5615.                x = 0;
  5616.  
  5617.             } else {
  5618.  
  5619.                if(B_XABS & upd->flags) n = x = xbegin + upd->ints[I_XOFS];
  5620.                else                    n = x = xbegin - upd->xprinter;
  5621.  
  5622.                if(        1 < upd->ints[I_XSTEP]) {
  5623.                   if(0 > n) {
  5624.                      n  -= upd->ints[I_XSTEP];
  5625.                      x  -= n;
  5626.                   }
  5627.                   if(n) n  /= upd->ints[I_XSTEP]; /* Major-Steps */
  5628.                   if(x) x  %= upd->ints[I_XSTEP]; /* Minor-Steps */
  5629.  
  5630.                } else if(-1 > upd->ints[I_XSTEP]) {
  5631.                   n *= -upd->ints[I_XSTEP]; /* May this work? */
  5632.                   x  = 0;
  5633.                }
  5634.  
  5635.                if(n) { /* Adjust X-Position */
  5636.  
  5637.                  memcpy(upd->outbuf+ioutbuf,
  5638.                              upd->strings[S_XMOVE].data,
  5639.                              upd->strings[S_XMOVE].size);
  5640.                   ioutbuf += upd->strings[S_XMOVE].size;
  5641.  
  5642.                   upd->outbuf[ioutbuf++] =  n     & 0xff;
  5643.                   upd->outbuf[ioutbuf++] = (n>>8) & 0xff;
  5644.  
  5645.                }       /* Adjust X-Position */
  5646.  
  5647.             }
  5648.  
  5649.             if(x && 0 < upd->strings[S_XSTEP].size) { /* Fine-Adjust X */
  5650.                while(x--) {
  5651.                   memcpy(upd->outbuf+ioutbuf,
  5652.                              upd->strings[S_XSTEP].data,
  5653.                              upd->strings[S_XSTEP].size);
  5654.                   ioutbuf += upd->strings[S_XSTEP].size;
  5655.                }
  5656.             }                                         /* Fine-Adjust X */
  5657.          }
  5658.          upd->xprinter = xend+1;
  5659.  
  5660. /*
  5661.  *       Send the Write-Command
  5662.  */
  5663.          if(0 < upd->string_a[SA_WRITECOMP].data[icomp].size) {
  5664.             memcpy(upd->outbuf+ioutbuf,
  5665.                        upd->string_a[SA_WRITECOMP].data[icomp].data,
  5666.                        upd->string_a[SA_WRITECOMP].data[icomp].size);
  5667.             ioutbuf += upd->string_a[SA_WRITECOMP].data[icomp].size;
  5668.          }
  5669.          n = xend + 1 - xbegin;
  5670.          upd->outbuf[ioutbuf++] = (n<<3) & 255;
  5671.          upd->outbuf[ioutbuf++] = (n>>5) & 255;
  5672. /*
  5673.  *       Set the Pixels
  5674.  */
  5675.          for(pin = 0; pin < pintop; ++pin) {
  5676.             ioutbuf += upd_rle(upd->outbuf+ioutbuf,NULL,n);
  5677.             fwrite(upd->outbuf,1,ioutbuf,out);
  5678.             ioutbuf = 0;
  5679.          }
  5680.  
  5681.          for(y = ybegin; 0 > y;    y += upd->ints[I_NYPASS]) {
  5682.             ioutbuf += upd_rle(upd->outbuf+ioutbuf,NULL,n);
  5683.             fwrite(upd->outbuf,1,ioutbuf,out);
  5684.             ioutbuf = 0;
  5685.          }
  5686.  
  5687.          for(; y < yend; y += upd->ints[I_NYPASS]) {
  5688.             ioutbuf += upd_rle(upd->outbuf+ioutbuf,
  5689.                upd->scnbuf[y & upd->scnmsk][icomp].bytes+xbegin,n);
  5690.             fwrite(upd->outbuf,1,ioutbuf,out);
  5691.             ioutbuf = 0;
  5692.          }
  5693.  
  5694.          for(pin = pinbot; pin < upd->ints[I_PINS2WRITE]; ++pin) {
  5695.             ioutbuf += upd_rle(upd->outbuf+ioutbuf,NULL,n);
  5696.             fwrite(upd->outbuf,1,ioutbuf,out);
  5697.             ioutbuf = 0;
  5698.          }
  5699.       }                                             /* Component-Print */
  5700.    }                    /* Some data to write */
  5701.  
  5702. /** Advance counters in upd, change modi */
  5703.    if(upd->yscan < upd->ints[I_BEG_Y]) {
  5704.       upd->yscan += upd->int_a[IA_BEG_DY].data[upd->ipass++];
  5705.       if(     upd->ints[I_BEG_Y] <= upd->yscan) upd->ipass = 0;
  5706.       else if(upd->int_a[IA_BEG_DY].size <= upd->ipass) upd->ipass = 0;
  5707.    } else if(upd->yscan >= upd->ints[I_END_Y]) {
  5708.       upd->yscan += upd->int_a[IA_END_DY].data[upd->ipass++];
  5709.       if(upd->int_a[IA_END_DY].size <= upd->ipass) upd->ipass = 0;
  5710.    } else {
  5711.       upd->yscan += upd->int_a[IA_STD_DY].data[upd->ipass++];
  5712.       if(upd->int_a[IA_STD_DY].size <= upd->ipass) upd->ipass = 0;
  5713.       if(upd->yscan >= upd->ints[I_END_Y])         upd->ipass = 0;
  5714.    }
  5715.  
  5716.    return 0;
  5717. }
  5718.  
  5719. /* ------------------------------------------------------------------- */
  5720. /* upd_wrtescp2x: Write an ESC/P2-pass with X-Weaving                  */
  5721. /* ------------------------------------------------------------------- */
  5722.  
  5723. private int
  5724. upd_wrtescp2x(upd_p upd, FILE *out)
  5725. {
  5726.    int  pinbot,pin,pintop,xbegin,x,xend,icomp,ybegin,yend,y,ioutbuf,n,ixpass;
  5727.    byte *obytes,bit;
  5728.    updscan_p scan;
  5729.  
  5730. /** Determine the number of pins to write */
  5731.  
  5732.    if(upd->yscan < upd->ints[I_BEG_Y]) {
  5733.       ixpass = upd->int_a[IA_BEG_IX].data[upd->ipass];
  5734.       pintop = 0;
  5735.       pinbot = upd->int_a[IA_BEGBOT].data[upd->ipass];
  5736.    } else if(upd->yscan >= upd->ints[I_END_Y]) {
  5737.       ixpass = upd->int_a[IA_END_IX].data[upd->ipass];
  5738.       pinbot = upd->ints[I_PINS2WRITE];
  5739.       pintop = pinbot - upd->int_a[IA_ENDTOP].data[upd->ipass];
  5740.    } else {
  5741.       ixpass = upd->int_a[IA_STD_IX].data[upd->ipass];
  5742.       pintop = 0;
  5743.       pinbot = upd->ints[I_PINS2WRITE];
  5744.    }
  5745.  
  5746.    ybegin =  pintop * upd->ints[I_NYPASS] + upd->yscan - upd->ints[I_BEGSKIP];
  5747.    yend   =  pinbot * upd->ints[I_NYPASS] + upd->yscan - upd->ints[I_BEGSKIP];
  5748.  
  5749. /** Determine Width of this scan */
  5750.  
  5751.    xbegin = upd->pwidth;
  5752.    xend   = -1;
  5753.  
  5754.    for(y = ybegin; y < yend; y += upd->ints[I_NYPASS]) { /* Pin-testloop */
  5755.  
  5756.       if(0 > y) continue; /* Inserted Scanlines */
  5757.  
  5758.       scan = upd->scnbuf[y & upd->scnmsk];
  5759.  
  5760.       for(icomp = 0; icomp < upd->ocomp; ++icomp) { /* Compwise test */
  5761.          if(xbegin > scan[icomp].xbegin[ixpass])
  5762.             xbegin = scan[icomp].xbegin[ixpass];
  5763.          if(xend   < scan[icomp].xend[  ixpass])
  5764.             xend   = scan[icomp].xend[  ixpass];
  5765.       }                                             /* Compwise test */
  5766.  
  5767.    }                                                     /* Pin-testloop */
  5768.  
  5769.    if(xbegin <= xend) { /* Some data to write */
  5770.  
  5771.       ioutbuf = upd->nbytes;
  5772.  
  5773.       if(0 == upd->strings[S_XMOVE].size) xbegin = ixpass;
  5774.  
  5775. /*
  5776.  *    Adjust the Printers Y-Position
  5777.  */
  5778.       if(upd->yscan != upd->yprinter) { /* Adjust Y-Position */
  5779.          if(B_YABS & upd->flags) y = upd->yscan + upd->ints[I_YOFS];
  5780.          else                    y = upd->yscan - upd->yprinter;
  5781.  
  5782.          if(      1 < upd->ints[I_YSTEP]) {
  5783.             n      =  y / upd->ints[I_YSTEP];  /* Major-Steps */
  5784.             y     -=  n * upd->ints[I_YSTEP];  /* Minor-Steps */
  5785.          } else if(-1 > upd->ints[I_YSTEP]) {
  5786.             n      = y * -upd->ints[I_YSTEP];  /* May this work? */
  5787.             y      = 0;
  5788.          } else {
  5789.             n      = y;
  5790.             y      = 0;
  5791.          }
  5792.  
  5793.          if(n) { /* Coarse Positioning */
  5794.             memcpy(upd->outbuf+ioutbuf,
  5795.                        upd->strings[S_YMOVE].data,upd->strings[S_YMOVE].size);
  5796.             ioutbuf += upd->strings[S_YMOVE].size;
  5797.  
  5798.             upd->outbuf[ioutbuf++] =  n     & 0xff;
  5799.             upd->outbuf[ioutbuf++] = (n>>8) & 0xff;
  5800.  
  5801.          }       /* Coarse Positioning */
  5802.  
  5803.          if(0 < upd->strings[S_YSTEP].size) {
  5804.             while(y--) {
  5805.                memcpy(upd->outbuf+ioutbuf,
  5806.                           upd->strings[S_YSTEP].data,
  5807.                           upd->strings[S_YSTEP].size);
  5808.                ioutbuf += upd->strings[S_YSTEP].size;
  5809.             }
  5810.          }
  5811.  
  5812.          upd->yprinter = upd->yscan;
  5813.       }                                 /* Adjust Y-Position */
  5814.  
  5815. /*
  5816.  * Now write the required components
  5817.  */
  5818.       for(icomp = 0; icomp < upd->ocomp; ++icomp) { /* Component-Print */
  5819. /*
  5820.  *       First check, wether this Component needs printing
  5821.  */
  5822.          for(y = ybegin; y < yend; y += upd->ints[I_NYPASS]) { /* Comp-Test */
  5823.             if(0 > y) continue;
  5824.             scan = upd->scnbuf[y & upd->scnmsk]+icomp;
  5825.             if(0 <= scan->xend[ixpass]) break;
  5826.          }                                                     /* Comp-Test */
  5827.          if(y >= yend) continue; /* Component not required */
  5828. /*
  5829.  *       Select the Component
  5830.  */
  5831.          if((0 < upd->string_a[SA_SETCOMP].size) &&
  5832.             (upd->icomp != icomp               )   ) { /* Selection enabled */
  5833.             upd->icomp = icomp;
  5834.             if(0 < upd->string_a[SA_SETCOMP].data[icomp].size) {
  5835.                memcpy(upd->outbuf+ioutbuf,
  5836.                           upd->string_a[SA_SETCOMP].data[icomp].data,
  5837.                           upd->string_a[SA_SETCOMP].data[icomp].size);
  5838.                ioutbuf += upd->string_a[SA_SETCOMP].data[icomp].size;
  5839.             }
  5840.          }                                      /* Selection enabled */
  5841. /*
  5842.  *       Establish the X-Position
  5843.  */
  5844.          if(xbegin != upd->xprinter) {
  5845.  
  5846.             if(0 == upd->strings[S_XMOVE].size) {
  5847.  
  5848.                upd->outbuf[ioutbuf++] = '\r';
  5849.                upd->xprinter          =  0;
  5850.                n = 0;
  5851.                x = ixpass;
  5852.  
  5853.             } else {
  5854.  
  5855.                if(B_XABS & upd->flags) n = x = xbegin + upd->ints[I_XOFS];
  5856.                else                    n = x = xbegin - upd->xprinter;
  5857.  
  5858.                if(        1 < upd->ints[I_XSTEP]) {
  5859.                   if(0 > n) {
  5860.                      n  -= upd->ints[I_XSTEP];
  5861.                      x  -= n;
  5862.                   }
  5863.                   if(n) n  /= upd->ints[I_XSTEP]; /* Major-Steps */
  5864.                   if(x) x  %= upd->ints[I_XSTEP]; /* Minor-Steps */
  5865.  
  5866.                } else if(-1 > upd->ints[I_XSTEP]) {
  5867.                   n *= -upd->ints[I_XSTEP]; /* May this work? */
  5868.                   x  = 0;
  5869.                }
  5870.  
  5871.                if(n) { /* Adjust X-Position */
  5872.  
  5873.                  memcpy(upd->outbuf+ioutbuf,
  5874.                              upd->strings[S_XMOVE].data,
  5875.                              upd->strings[S_XMOVE].size);
  5876.                   ioutbuf += upd->strings[S_XMOVE].size;
  5877.  
  5878.                   upd->outbuf[ioutbuf++] =  n     & 0xff;
  5879.                   upd->outbuf[ioutbuf++] = (n>>8) & 0xff;
  5880.  
  5881.                }       /* Adjust X-Position */
  5882.  
  5883.             }
  5884.  
  5885.             if(x && 0 < upd->strings[S_XSTEP].size) { /* Fine-Adjust X */
  5886.                while(x--) {
  5887.                   memcpy(upd->outbuf+ioutbuf,
  5888.                              upd->strings[S_XSTEP].data,
  5889.                              upd->strings[S_XSTEP].size);
  5890.                   ioutbuf += upd->strings[S_XSTEP].size;
  5891.                }
  5892.             }                                         /* Fine-Adjust X */
  5893.          }
  5894.          upd->xprinter = xend+1;
  5895.  
  5896. /*
  5897.  *       Send the Write-Command
  5898.  */
  5899.          if(0 < upd->string_a[SA_WRITECOMP].data[icomp].size) {
  5900.             memcpy(upd->outbuf+ioutbuf,
  5901.                        upd->string_a[SA_WRITECOMP].data[icomp].data,
  5902.                        upd->string_a[SA_WRITECOMP].data[icomp].size);
  5903.             ioutbuf += upd->string_a[SA_WRITECOMP].data[icomp].size;
  5904.          }
  5905.          n = ((xend - xbegin) / upd->ints[I_NXPASS] + 8) & ~7;
  5906.          upd->outbuf[ioutbuf++] =  n     & 255;
  5907.          upd->outbuf[ioutbuf++] = (n>>8) & 255;
  5908.          n >>= 3;
  5909. /*
  5910.  *       Set the Pixels
  5911.  */
  5912.          for(pin = 0; pin < pintop; ++pin) {
  5913.             ioutbuf += upd_rle(upd->outbuf+ioutbuf,NULL,n);
  5914.             fwrite(upd->outbuf+upd->nbytes,1,ioutbuf-upd->nbytes,out);
  5915.             ioutbuf = upd->nbytes;
  5916.          }
  5917.  
  5918.          for(y = ybegin; 0 > y;    y += upd->ints[I_NYPASS]) {
  5919.             ioutbuf += upd_rle(upd->outbuf+ioutbuf,NULL,n);
  5920.             fwrite(upd->outbuf+upd->nbytes,1,ioutbuf-upd->nbytes,out);
  5921.             ioutbuf = upd->nbytes;
  5922.          }
  5923.  
  5924.          for(;           y < yend; y += upd->ints[I_NYPASS]) {
  5925.             byte * ibytes = upd->scnbuf[y & upd->scnmsk][icomp].bytes;
  5926.             obytes = upd->outbuf;
  5927.             memset(obytes,0,upd->nbytes);
  5928.             bit = 0x80;
  5929.             for(x = xbegin; x <= xend; x += upd->ints[I_NXPASS]) {
  5930.                if(ibytes[x>>3] & (0x80 >> (x & 7))) *obytes |= bit;
  5931.                if(!(bit >>= 1)) { obytes++; bit = 0x80; }
  5932.             }
  5933.             ioutbuf += upd_rle(upd->outbuf+ioutbuf,upd->outbuf,n);
  5934.             fwrite(upd->outbuf+upd->nbytes,1,ioutbuf-upd->nbytes,out);
  5935.             ioutbuf = upd->nbytes;
  5936.          }
  5937.  
  5938.          for(pin = pinbot; pin < upd->ints[I_PINS2WRITE]; ++pin) {
  5939.             ioutbuf += upd_rle(upd->outbuf+ioutbuf,NULL,n);
  5940.             fwrite(upd->outbuf+upd->nbytes,1,ioutbuf-upd->nbytes,out);
  5941.             ioutbuf = upd->nbytes;
  5942.          }
  5943.       }                                             /* Component-Print */
  5944.    }                    /* Some data to write */
  5945.  
  5946. /** Advance counters in upd, change modi */
  5947.  
  5948.    if(upd->yscan < upd->ints[I_BEG_Y]) {
  5949.       upd->yscan += upd->int_a[IA_BEG_DY].data[upd->ipass++];
  5950.       if(     upd->ints[I_BEG_Y] <= upd->yscan) upd->ipass = 0;
  5951.       else if(upd->int_a[IA_BEG_DY].size <= upd->ipass) upd->ipass = 0;
  5952.    } else if(upd->yscan >= upd->ints[I_END_Y]) {
  5953.       upd->yscan += upd->int_a[IA_END_DY].data[upd->ipass++];
  5954.       if(upd->int_a[IA_END_DY].size <= upd->ipass) upd->ipass = 0;
  5955.    } else {
  5956.       upd->yscan += upd->int_a[IA_STD_DY].data[upd->ipass++];
  5957.       if(upd->int_a[IA_STD_DY].size <= upd->ipass) upd->ipass = 0;
  5958.       if(upd->yscan >= upd->ints[I_END_Y])         upd->ipass = 0;
  5959.    }
  5960.  
  5961.    return 0;
  5962. }
  5963.  
  5964. /* ------------------------------------------------------------------- */
  5965. /* upd_rle: The Runlength-Compressor                                   */
  5966. /* ------------------------------------------------------------------- */
  5967.  
  5968. private int
  5969. upd_rle(byte *out,const byte *in,int nbytes)
  5970. {
  5971.  
  5972.    int used = 0;
  5973.    int crun,cdata;
  5974.    byte run;
  5975.  
  5976.    if(in != NULL) { /* Data present */
  5977.  
  5978.       crun = 1;
  5979.  
  5980.       while(nbytes > 0) { /* something to compress */
  5981.  
  5982.          run = in[0];
  5983.  
  5984.          while((nbytes > crun) && (run == in[crun])) if(++crun == 128) break;
  5985.  
  5986.          if((crun > 2) || (crun == nbytes)) { /* use this run */
  5987.  
  5988.             *out++  = (257 - crun) & 0xff; *out++ = run; used += 2;
  5989.  
  5990.             nbytes -= crun; in    += crun;
  5991.             crun = 1;
  5992.  
  5993.          } else {                            /* ignore this run */
  5994.  
  5995.             for(cdata = crun; (nbytes > cdata) && (crun < 4);) {
  5996.                if(run  == in[cdata]) crun += 1;
  5997.                else run = in[cdata], crun  = 1;
  5998.                if(++cdata == 128) break;
  5999.             }
  6000.  
  6001.             if(crun < 3) crun   = 0;    /* ignore trailing run */
  6002.             else         cdata -= crun;
  6003.  
  6004.             *out++ = cdata-1;     used++;
  6005.             memcpy(out,in,cdata); used += cdata; out   += cdata;
  6006.  
  6007.             nbytes -= cdata; in    += cdata;
  6008.  
  6009.          }              /* use/ignore run */
  6010.  
  6011.       }                  /* something to compress */
  6012.  
  6013.    } else {         /* Empty scans to fill bands */
  6014.  
  6015.       while(nbytes > 0) {
  6016.          crun    = nbytes > 128 ? 128 : nbytes;
  6017.          nbytes -= crun;
  6018.          *out++  = (257 - crun) & 0xff;
  6019.          *out++  = 0;
  6020.          used   += 2;
  6021.       }
  6022.    }                /* Data present or empty */
  6023.    return used;
  6024. }
  6025.  
  6026. /* ------------------------------------------------------------------- */
  6027. /* upd_open_wrtrtl: Basic HP-RTL Writer                                */
  6028. /* ------------------------------------------------------------------- */
  6029.  
  6030. private int
  6031. upd_open_wrtrtl(upd_device *udev)
  6032. {
  6033.    const upd_p      upd  = udev->upd;
  6034.    int              error = 0;
  6035.  
  6036. /** Adjust the Raster-Width */
  6037.  
  6038.    if(0 < upd->strings[S_BEGIN].size) { /* BOP-Checker */
  6039.  
  6040.      int   i,j,state = 0;
  6041.      char  cv[24];
  6042.      byte  *bp;
  6043.      uint  ncv,nbp;
  6044.  
  6045.      j = -1;
  6046.      for(i = 0; i < upd->strings[S_BEGIN].size; ++i) {
  6047.        const int c = upd->strings[S_BEGIN].data[i];
  6048.  
  6049.        switch(state) {
  6050.          case  0:
  6051.            if(        c == 0x1b) state =  1; /* ESC */
  6052.          break;
  6053.  
  6054.          case  1:
  6055.            if(        c == 0x2a) state =  2; /* ESC * */
  6056.            else if(   c == 0x25) state =  5; /* ESC % */
  6057.            else                  state =  0;
  6058.          break;
  6059.  
  6060.          case  2:
  6061.            j = i; /* This character is not part of the replaced text */
  6062.            if(        c == 0x72) state =  3; /* ESC * r */
  6063.            else if(   c == 0x74) state =  4; /* ESC * t */
  6064.            else                  state =  0;
  6065.          break;
  6066.  
  6067.          case  3:
  6068.  
  6069.            if(       (B_PAGEWIDTH  & upd->flags) &&
  6070.                      ((c == 0x73) || (c == 0x53))  ) { /* esc * r # S */
  6071.  
  6072.              sprintf(cv,"%d",upd->pwidth);
  6073.              ncv = strlen(cv);
  6074.  
  6075.              nbp = (j+1) + ncv + (upd->strings[S_BEGIN].size-i);
  6076.              UPD_MM_GET_ARRAY(bp,nbp);
  6077.  
  6078.              if(0 <= j) memcpy(bp,upd->strings[S_BEGIN].data,j+1);
  6079.              memcpy(bp+j+1,    cv,ncv);
  6080.              memcpy(bp+j+1+ncv,upd->strings[S_BEGIN].data+i,
  6081.                                upd->strings[S_BEGIN].size-i);
  6082.              i = j+1+ncv;
  6083.              UPD_MM_DEL_PARAM(upd->strings[S_BEGIN]);
  6084.              upd->strings[S_BEGIN].data = bp;
  6085.              upd->strings[S_BEGIN].size = nbp;
  6086.  
  6087.            } else if((B_PAGELENGTH & upd->flags) &&
  6088.                      ((c == 0x74) || (c == 0x54))  ) { /* esc * r # T */
  6089.  
  6090.              sprintf(cv,"%d",upd->pheight);
  6091.              ncv = strlen(cv);
  6092.  
  6093.              nbp = (j+1) + ncv + (upd->strings[S_BEGIN].size-i);
  6094.              UPD_MM_GET_ARRAY(bp,nbp);
  6095.  
  6096.              if(0 <= j) memcpy(bp,upd->strings[S_BEGIN].data,j+1);
  6097.              memcpy(bp+j+1,    cv,ncv);
  6098.              memcpy(bp+j+1+ncv,upd->strings[S_BEGIN].data+i,
  6099.                                upd->strings[S_BEGIN].size-i);
  6100.              i = j+1+ncv;
  6101.              UPD_MM_DEL_PARAM(upd->strings[S_BEGIN]);
  6102.              upd->strings[S_BEGIN].data = bp;
  6103.              upd->strings[S_BEGIN].size = nbp;
  6104.  
  6105.            }
  6106.  
  6107.            if(       (0x40 < c) && (c < 0x5b))  state = 0; /* Term. cmd. */
  6108.            else if(!((0x2f < c) && (c < 0x3a))) j     = i; /* Non-Number */
  6109.  
  6110.          break;
  6111.            
  6112.          case  4: /* esc * t */
  6113.  
  6114.            if(        (B_RESOLUTION  & upd->flags) &&
  6115.                      ((c == 0x72) || (c == 0x52))  ) { /* esc * t # R */
  6116.  
  6117.              sprintf(cv,"%d",(int)
  6118.                ((udev->y_pixels_per_inch < udev->x_pixels_per_inch ?
  6119.                  udev->x_pixels_per_inch : udev->y_pixels_per_inch)
  6120.                +0.5));
  6121.              ncv = strlen(cv);
  6122.  
  6123.              nbp = (j+1) + ncv + (upd->strings[S_BEGIN].size-i);
  6124.              UPD_MM_GET_ARRAY(bp,nbp);
  6125.  
  6126.              if(0 <= j) memcpy(bp,upd->strings[S_BEGIN].data,j+1);
  6127.              memcpy(bp+j+1,    cv,ncv);
  6128.              memcpy(bp+j+1+ncv,upd->strings[S_BEGIN].data+i,
  6129.                                upd->strings[S_BEGIN].size-i);
  6130.              i = j+1+ncv;
  6131.              UPD_MM_DEL_PARAM(upd->strings[S_BEGIN]);
  6132.              upd->strings[S_BEGIN].data = bp;
  6133.              upd->strings[S_BEGIN].size = nbp;
  6134.  
  6135.            }
  6136.  
  6137.            if(       (0x40 < c) && (c < 0x5b))  state = 0; /* Term. cmd. */
  6138.            else if(!((0x2f < c) && (c < 0x3a))) j     = i; /* Non-Number */
  6139.  
  6140.          break;
  6141.  
  6142.          case  5: /* ESC % - 1 2 3 4 5 X */
  6143.            if( c == 0x2d) state =  6; /* ESC % - */
  6144.            else           state =  0;
  6145.          break;
  6146.  
  6147.          case  6: /* ESC %  - 1 2 3 4 5 X */
  6148.            if( c == 0x31) state =  7; /* ESC % - 1 */
  6149.            else           state =  0;
  6150.          break;
  6151.  
  6152.          case  7: /* ESC %  - 1 2 3 4 5 X */
  6153.            if( c == 0x32) state =  8; /* ESC % - 1 2 */
  6154.            else           state =  0;
  6155.          break;
  6156.  
  6157.          case  8: /* ESC %  - 1 2 3 4 5 X */
  6158.            if( c == 0x33) state =  9; /* ESC % - 1 2 3 */
  6159.            else           state =  0;
  6160.          break;
  6161.  
  6162.          case  9: /* ESC %  - 1 2 3 4 5 X */
  6163.            if( c == 0x34) state = 10; /* ESC % - 1 2 3 4 */
  6164.            else           state =  0;
  6165.          break;
  6166.  
  6167.          case 10: /* ESC %  - 1 2 3 4 5 X */
  6168.            if( c == 0x35) state = 11; /* ESC % - 1 2 3 4 5 */
  6169.            else           state =  0;
  6170.          break;
  6171.  
  6172.          case 11: /* ESC %  - 1 2 3 4 5 X */
  6173.            if( c == 0x58) state = 12; /* ESC % - 1 2 3 4 5 X */
  6174.            else           state =  0;
  6175.          break;
  6176.  
  6177.          case 12: /* PJL-BOL:  @ P J L ws */
  6178.            if( c == 0x40) state = 13; /* @ */
  6179.            else           state =  0;
  6180.          break;
  6181.  
  6182.          case 13: /* PJL-BOL  @ P J L ws */
  6183.            if( c == 0x50) state = 14; /* @ P */
  6184.            else           state =  0;
  6185.          break;
  6186.  
  6187.          case 14: /* PJL-BOL  @ P J L ws */
  6188.            if( c == 0x4a) state = 15; /* @ P J */
  6189.            else           state =  0;
  6190.          break;
  6191.  
  6192.          case 15: /* PJL-BOL  @ P J L ws */
  6193.            if( c == 0x4c) state = 16; /* @ P J L */
  6194.            else           state =  0;
  6195.          break;
  6196.  
  6197.          case 16: /* PJL-BOL  @ P J L ws */
  6198.            if((c == 0x20) || (c == 0x09)) state = 19; /* @ P J L ws */
  6199.            else if(           c == 0x0d ) state = 17;
  6200.            else if(           c == 0x0a ) state = 12;
  6201.            else                           state =  0; /* PJL-Error */
  6202.          break;
  6203.  
  6204.          case 17: /* PJL-EOL  */
  6205.            if( c == 0x0a) state = 12; /* Next PJL-Command */
  6206.            else           state =  0; /* PJL-Error */
  6207.          break;
  6208.  
  6209.          case 18: /* PJL-Eatup: Expect Newline */
  6210.            if( c == 0x0a) state = 12;
  6211.          break;
  6212.  
  6213.          case 19: /* Begin of PJL-Command */
  6214.            if(     (c == 0x53) || (c == 0x73)) state = 20; /* S E T*/
  6215.            else if( c == 0x0a                ) state = 12; /* BOL */
  6216.            else if( c == 0x0d                ) state = 17;
  6217.          break;
  6218.  
  6219.          case 20: /* PJL-Set: S E T  */
  6220.            if(     (c == 0x45) || (c == 0x65)) state = 21; /* S E */
  6221.            else if( c == 0x0a                ) state = 12; /* BOL */
  6222.            else                                state = 18;
  6223.          break;
  6224.  
  6225.          case 21: /* PJL-Set: S E T  */
  6226.            if(     (c == 0x54) || (c == 0x74)) state = 22; /* S E T */
  6227.            else if( c == 0x0a                ) state = 12; /* BOL */
  6228.            else                                state = 18;
  6229.          break;
  6230.  
  6231.          case 22: /* PJL-Set: S E T ws  */
  6232.            if(     (c == 0x20) || (c == 0x09)) state = 23; /* S E T ws */
  6233.            else if( c == 0x0a                ) state = 12; /* BOL */
  6234.            else                                state = 18;
  6235.          break;
  6236.  
  6237.          case 23: /* PJL-Set: S E T ws  */
  6238.            if(     (c == 0x50) || (c == 0x70)) state = 24; /* set paper... */
  6239.            else if((c == 0x52) || (c == 0x72)) state = 41; /* set resolution */
  6240.            else if( c == 0x0a                ) state = 12; /* BOL */
  6241.            else                                state = 18;
  6242.          break;
  6243.  
  6244.          case 24: /* PJL-Set: set paper...  */
  6245.            if(     (c == 0x41) || (c == 0x61)) state = 25; /* set pa */
  6246.            else if( c == 0x0a                ) state = 12; /* BOL */
  6247.            else                                state = 18;
  6248.          break;
  6249.  
  6250.          case 25: /* PJL-Set: set paper...  */
  6251.            if(     (c == 0x50) || (c == 0x70)) state = 26; /* set pap */
  6252.            else if( c == 0x0a                ) state = 12; /* BOL */
  6253.            else                                state = 18;
  6254.          break;
  6255.  
  6256.          case 26: /* PJL-Set: set paper...  */
  6257.            if(     (c == 0x45) || (c == 0x65)) state = 27; /* set pape */
  6258.            else if( c == 0x0a                ) state = 12; /* BOL */
  6259.            else                                state = 18;
  6260.          break;
  6261.  
  6262.          case 27: /* PJL-Set: set paper...  */
  6263.            if(     (c == 0x52) || (c == 0x72)) state = 28; /* set paper */
  6264.            else if( c == 0x0a                ) state = 12; /* BOL */
  6265.            else                                state = 18;
  6266.          break;
  6267.  
  6268.          case 28: /* PJL-Set: set paper?  */
  6269.            if(     (c == 0x4c) || (c == 0x6c)) state = 29; /* set paperlength */
  6270.            else if((c == 0x57) || (c == 0x77)) state = 36; /* set paperwidth */
  6271.            else if( c == 0x0a                ) state = 12; /* BOL */
  6272.            else                                state = 18;
  6273.          break;
  6274.  
  6275.          case 29: /* PJL: set paperlength  */
  6276.            if(     (c == 0x45) || (c == 0x65)) state = 30; /* set paperle */
  6277.            else if( c == 0x0a                ) state = 12; /* BOL */
  6278.            else                                state = 18;
  6279.          break;
  6280.  
  6281.          case 30: /* PJL: set paperlength  */
  6282.            if(     (c == 0x4e) || (c == 0x6e)) state = 31; /* set paperlen */
  6283.            else if( c == 0x0a                ) state = 12; /* BOL */
  6284.            else                                state = 18;
  6285.          break;
  6286.  
  6287.          case 31: /* PJL: set paperlength  */
  6288.            if(     (c == 0x47) || (c == 0x67)) state = 32; /* set paperleng */
  6289.            else if( c == 0x0a                ) state = 12; /* BOL */
  6290.            else                                state = 18;
  6291.          break;
  6292.  
  6293.          case 32: /* PJL: set paperlength  */
  6294.            if(     (c == 0x54) || (c == 0x74)) state = 33; /* set paperlengt */
  6295.            else if( c == 0x0a                ) state = 12; /* BOL */
  6296.            else                                state = 18;
  6297.          break;
  6298.  
  6299.          case 33: /* PJL: set paperlength  */
  6300.            if(     (c == 0x48) || (c == 0x68)) state = 34; /* set paperlength */
  6301.            else if( c == 0x0a                ) state = 12; /* BOL */
  6302.            else                                state = 18;
  6303.          break;
  6304.  
  6305.          case 34: /* PJL: set paperlength  */
  6306.            if(      c == 0x3d                ) state = 35; /* set paperlength */
  6307.            else if( c == 0x0a                ) state = 12; /* BOL */
  6308.            else if((c != 0x20) && (c != 0x09)) state = 18;
  6309.          break;
  6310.  
  6311.          case 35: /* PJL: set paperlength */
  6312.            if(c == 0x30) { /* Replace this Zero */
  6313.  
  6314.              sprintf(cv,"%d",(int)
  6315.                (720.0 * udev->height / udev->y_pixels_per_inch));
  6316.              ncv = strlen(cv);
  6317.  
  6318.              nbp = upd->strings[S_BEGIN].size + ncv - 1;
  6319.              UPD_MM_GET_ARRAY(bp,nbp);
  6320.  
  6321.              if(0 < i) memcpy(bp,upd->strings[S_BEGIN].data,i);
  6322.              memcpy(bp+i,cv,ncv);
  6323.              if(upd->strings[S_BEGIN].size > (i+1))
  6324.                memcpy(bp+i+ncv,upd->strings[S_BEGIN].data+i+1,
  6325.                                upd->strings[S_BEGIN].size-i-1);
  6326.  
  6327.              i += ncv - 1;
  6328.              UPD_MM_DEL_PARAM(upd->strings[S_BEGIN]);
  6329.              upd->strings[S_BEGIN].data = bp;
  6330.              upd->strings[S_BEGIN].size = nbp;
  6331.  
  6332.            } else if(              c == 0x0a )                state = 12;
  6333.            else if((c != 0x20) && (c != 0x09) && (c != 0x3d)) state = 18;
  6334.  
  6335.          break;
  6336.  
  6337.          case 36: /* PJL: set paperwidth  */
  6338.            if(     (c == 0x49) || (c == 0x69)) state = 37; /* set paperwi */
  6339.            else if( c == 0x0a                ) state = 12; /* BOL */
  6340.            else                                state = 18;
  6341.          break;
  6342.  
  6343.          case 37: /* PJL: set paperwidth */
  6344.            if(     (c == 0x44) || (c == 0x64)) state = 38; /* set paperwid */
  6345.            else if( c == 0x0a                ) state = 12; /* BOL */
  6346.            else                                state = 18;
  6347.          break;
  6348.  
  6349.          case 38: /* PJL: set paperwidth  */
  6350.            if(     (c == 0x54) || (c == 0x74)) state = 39; /* set paperwidt */
  6351.            else if( c == 0x0a                ) state = 12; /* BOL */
  6352.            else                                state = 18;
  6353.          break;
  6354.  
  6355.          case 39: /* PJL: set paperwidth  */
  6356.            if(     (c == 0x48) || (c == 0x68)) state = 40; /* set paperwidth */
  6357.            else if( c == 0x0a                ) state = 12; /* BOL */
  6358.            else                                state = 18;
  6359.          break;
  6360.  
  6361.          case 40: /* PJL: set paperwidth */
  6362.            if(c == 0x30) { /* Replace this Zero */
  6363.  
  6364.              sprintf(cv,"%d",(int)
  6365.                (720.0 * udev->width / udev->x_pixels_per_inch));
  6366.              ncv = strlen(cv);
  6367.  
  6368.              nbp = upd->strings[S_BEGIN].size + ncv - 1;
  6369.              UPD_MM_GET_ARRAY(bp,nbp);
  6370.  
  6371.              if(0 < i) memcpy(bp,upd->strings[S_BEGIN].data,i);
  6372.              memcpy(bp+i,cv,ncv);
  6373.              if(upd->strings[S_BEGIN].size > (i+1))
  6374.                memcpy(bp+i+ncv,upd->strings[S_BEGIN].data+i+1,
  6375.                                upd->strings[S_BEGIN].size-i-1);
  6376.  
  6377.              i += ncv - 1;
  6378.              UPD_MM_DEL_PARAM(upd->strings[S_BEGIN]);
  6379.              upd->strings[S_BEGIN].data = bp;
  6380.              upd->strings[S_BEGIN].size = nbp;
  6381.  
  6382.            } else if(              c == 0x0a )                state = 12;
  6383.            else if((c != 0x20) && (c != 0x09) && (c != 0x3d)) state = 18;
  6384.  
  6385.          break;
  6386.  
  6387.          case 41: /* PJL: set resolution */
  6388.            if(     (c == 0x45) || (c == 0x65)) state = 42; /* set re */
  6389.            else if( c == 0x0a                ) state = 12; /* BOL */
  6390.            else                                state = 18;
  6391.          break;
  6392.  
  6393.          case 42: /* PJL: set resolution */
  6394.            if(     (c == 0x53) || (c == 0x73)) state = 43; /* set res */
  6395.            else if( c == 0x0a                ) state = 12; /* BOL */
  6396.            else                                state = 18;
  6397.          break;
  6398.  
  6399.          case 43: /* PJL: set resolution */
  6400.            if(     (c == 0x4f) || (c == 0x6f)) state = 44; /* set reso */
  6401.            else if( c == 0x0a                ) state = 12; /* BOL */
  6402.            else                                state = 18;
  6403.          break;
  6404.  
  6405.          case 44: /* PJL: set resolution */
  6406.            if(     (c == 0x4c) || (c == 0x6c)) state = 45; /* set resol */
  6407.            else if( c == 0x0a                ) state = 12; /* BOL */
  6408.            else                                state = 18;
  6409.          break;
  6410.  
  6411.          case 45: /* PJL: set resolution */
  6412.            if(     (c == 0x55) || (c == 0x75)) state = 46; /* set resolu */
  6413.            else if( c == 0x0a                ) state = 12; /* BOL */
  6414.            else                                state = 18;
  6415.          break;
  6416.  
  6417.          case 46: /* PJL: set resolution */
  6418.            if(     (c == 0x54) || (c == 0x74)) state = 47; /* set resolut */
  6419.            else if( c == 0x0a                ) state = 12; /* BOL */
  6420.            else                                state = 18;
  6421.          break;
  6422.  
  6423.          case 47: /* PJL: set resolution */
  6424.            if(     (c == 0x49) || (c == 0x69)) state = 48; /* set resoluti */
  6425.            else if( c == 0x0a                ) state = 12; /* BOL */
  6426.            else                                state = 18;
  6427.          break;
  6428.  
  6429.          case 48: /* PJL: set resolution */
  6430.            if(     (c == 0x4f) || (c == 0x6f)) state = 49; /* set resolutio */
  6431.            else if( c == 0x0a                ) state = 12; /* BOL */
  6432.            else                                state = 18;
  6433.          break;
  6434.  
  6435.          case 49: /* PJL: set resolution */
  6436.            if(     (c == 0x4e) || (c == 0x6e)) state = 50; /* set resolution */
  6437.            else if( c == 0x0a                ) state = 12; /* BOL */
  6438.            else                                state = 18;
  6439.          break;
  6440.  
  6441.          case 50: /* PJL: set resolution */
  6442.            if(c == 0x30) { /* Replace this Zero */
  6443.  
  6444.              sprintf(cv,"%d",(int)
  6445.                ((udev->y_pixels_per_inch < udev->x_pixels_per_inch ?
  6446.                  udev->x_pixels_per_inch : udev->y_pixels_per_inch)
  6447.                +0.5));
  6448.              ncv = strlen(cv);
  6449.  
  6450.              nbp = upd->strings[S_BEGIN].size + ncv - 1;
  6451.              UPD_MM_GET_ARRAY(bp,nbp);
  6452.  
  6453.              if(0 < i) memcpy(bp,upd->strings[S_BEGIN].data,i);
  6454.              memcpy(bp+i,cv,ncv);
  6455.              if(upd->strings[S_BEGIN].size > (i+1))
  6456.                memcpy(bp+i+ncv,upd->strings[S_BEGIN].data+i+1,
  6457.                                upd->strings[S_BEGIN].size-i-1);
  6458.  
  6459.              i += ncv - 1;
  6460.              UPD_MM_DEL_PARAM(upd->strings[S_BEGIN]);
  6461.              upd->strings[S_BEGIN].data = bp;
  6462.              upd->strings[S_BEGIN].size = nbp;
  6463.  
  6464.            } else if(              c == 0x0a )                state = 12;
  6465.            else if((c != 0x20) && (c != 0x09) && (c != 0x3d)) state = 18;
  6466.  
  6467.          break;
  6468.  
  6469.          default:
  6470. #if UPD_MESSAGES & UPD_M_ERROR
  6471.            fprintf(stderr,"UNIPRINT-Coding error, wrrtl, state = %d\n",state);
  6472. #endif
  6473.            state = 0;
  6474.          break;
  6475.        }
  6476.      }
  6477.    }                                    /* BOP-Checker */
  6478.  
  6479. /** SA_WRITECOMP must be valid */
  6480.    if(upd->ocomp > upd->string_a[SA_WRITECOMP].size) {
  6481. #if UPD_MESSAGES & UPD_M_WARNING
  6482.       fprintf(stderr,
  6483.          "PCL-Open: WRITECOMP-Commands must be given\n");
  6484. #endif
  6485.       error = -1;
  6486.    }
  6487.  
  6488. /**
  6489. If all this is correct, it's time to compute the size of the output-buffer.
  6490. It must hold:
  6491.   1. Y-Positioning
  6492.   2. Component-Data
  6493. */
  6494.    if(0 <= error) {
  6495.       int32 ny,noutbuf;
  6496.       char  tmp[16];
  6497.  
  6498.       if(0 < upd->strings[S_YMOVE].size) {
  6499.          sprintf(tmp,"%d",upd->pheight);
  6500.          ny = upd->strings[S_YMOVE].size + strlen(tmp);
  6501.       } else {
  6502.          ny = 1 + upd->string_a[SA_WRITECOMP].data[upd->ocomp-1].size;
  6503.          ny *= upd->pheight;
  6504.       }
  6505.  
  6506.       noutbuf = upd->nbytes + (upd->nbytes + 127) / 128;
  6507.  
  6508.       if(ny > noutbuf) noutbuf = ny;
  6509.       noutbuf += 16;
  6510.  
  6511.       if((0 < noutbuf) && (noutbuf <= INT_MAX)) {
  6512.          upd->noutbuf      = noutbuf;
  6513.          upd->writer       = upd_wrtrtl;
  6514.          error             = 1;
  6515.       } else {
  6516.          error = -1;
  6517. #if      UPD_MESSAGES & UPD_M_WARNING
  6518.             fprintf(stderr,
  6519.               "PCL-Open: %ld is unreasonable size of Outputbuffer\n",
  6520.               (long) noutbuf);
  6521. #endif
  6522.       }
  6523.    }
  6524.  
  6525.    return error;
  6526. }
  6527.  
  6528. /* ------------------------------------------------------------------- */
  6529. /* upd_wrtrtl: Write a pass                                            */
  6530. /* ------------------------------------------------------------------- */
  6531.  
  6532. private int
  6533. upd_wrtrtl(upd_p upd, FILE *out)
  6534. {
  6535.    const updscan_p scan = upd->scnbuf[upd->yscan & upd->scnmsk];
  6536.  
  6537.    int  x,xend,icomp,ioutbuf;
  6538.    byte *data;
  6539.  
  6540. /** Determine Width of this scan */
  6541.  
  6542.    xend   = -1;
  6543.    for(icomp = 0; icomp < upd->ocomp; ++icomp) {
  6544.  
  6545.       data = scan[icomp].bytes;
  6546.  
  6547.       for(x = upd->nbytes-1; 0 <= x; --x) if(data[x]) break;
  6548.       if(x > xend) xend  = x;
  6549.    }
  6550.  
  6551.    if(0 <= xend) { /* Some data to write */
  6552.  
  6553.       ioutbuf = 0;
  6554.       xend   += 1;
  6555. /*
  6556.  *    Adjust the Printers Y-Position
  6557.  */
  6558.       if(upd->yscan != upd->yprinter) { /* Adjust Y-Position */
  6559.          if(1 < upd->strings[S_YMOVE].size) {
  6560.             sprintf((char *)upd->outbuf+ioutbuf,
  6561.                (char *) upd->strings[S_YMOVE].data,upd->yscan - upd->yprinter);
  6562.             ioutbuf += strlen((char *)upd->outbuf+ioutbuf);
  6563.          } else {
  6564.             while(upd->yscan > upd->yprinter) {
  6565.                for(icomp = 0; icomp < upd->ocomp; ++icomp) {
  6566.                   sprintf((char *)upd->outbuf+ioutbuf,
  6567.                      (char *) upd->string_a[SA_WRITECOMP].data[icomp].data,0);
  6568.                   ioutbuf += strlen((char *)upd->outbuf+ioutbuf);
  6569.                }
  6570.                fwrite(upd->outbuf,1,ioutbuf,out);
  6571.                ioutbuf = 0;
  6572.                upd->yprinter += 1;
  6573.             }
  6574.          }
  6575.          upd->yprinter = upd->yscan;
  6576.          fwrite(upd->outbuf,1,ioutbuf,out);
  6577.          ioutbuf = 0;
  6578.       }                                 /* Adjust Y-Position */
  6579. /*
  6580.  * Now write the all! components
  6581.  */
  6582.       for(icomp = 0; icomp < upd->ocomp; ++icomp) { /* Component-Print */
  6583.          data = scan[icomp].bytes;
  6584.          for(x = 0; x <= xend; ++x) if(data[x]) break;
  6585.          if(x <= xend) {
  6586.             ioutbuf = upd_rle(upd->outbuf,scan[icomp].bytes,xend);
  6587.             fprintf(out,
  6588.                (char *)upd->string_a[SA_WRITECOMP].data[icomp].data,ioutbuf);
  6589.             fwrite(upd->outbuf,1,ioutbuf,out);
  6590.          } else {
  6591.             fprintf(out,
  6592.                (char *)upd->string_a[SA_WRITECOMP].data[icomp].data,0);
  6593.          }
  6594.       }
  6595.  
  6596.       upd->yprinter += 1;
  6597.  
  6598.    }                    /* Some data to write */
  6599.  
  6600. /** Advance scan by one */
  6601.  
  6602.    upd->yscan += 1;
  6603.  
  6604.    return 0;
  6605. }
  6606.  
  6607. /* ------------------------------------------------------------------- */
  6608. /* upd_open_wrtcanon: Basic Canon Extended Mode Writer (hr)            */
  6609. /* ------------------------------------------------------------------- */
  6610.  
  6611. private int
  6612. upd_open_wrtcanon(upd_device *udev)
  6613. {
  6614.   const upd_p upd = udev->upd;
  6615.   int error = 0;
  6616.  
  6617.   /* max length of one printer line */
  6618.   upd->noutbuf = upd->nbytes + (upd->nbytes + 127) / 128;
  6619.   upd->writer  = upd_wrtcanon;
  6620.   error        = 1;
  6621.  
  6622.   return error;
  6623. }
  6624.  
  6625. /* ------------------------------------------------------------------- */
  6626. /* upd_wrtcanon: Write a pass (hr)                                     */
  6627. /* ------------------------------------------------------------------- */
  6628.  
  6629. #define LOW(b)     ((b)&0xFF)
  6630. #define HIGH(b)    ((b)>>8)
  6631. #define ESC 0x1B
  6632. #define CR  0x0D
  6633.  
  6634. private int
  6635. upd_wrtcanon(upd_p upd, FILE *out)
  6636. {
  6637.   const updscan_p scan = upd->scnbuf[upd->yscan & upd->scnmsk];
  6638.  
  6639.   int x, xend, icomp, ioutbuf, step, ioutbuf1;
  6640.   byte *data;
  6641.  
  6642.  
  6643.   /* Check length of the printable date */
  6644.   xend = -1;
  6645.   for(icomp = 0; icomp < upd->ocomp; ++icomp) {
  6646.     data = scan[icomp].bytes;
  6647.  
  6648.     for(x = upd->nbytes-1; 0 <= x; --x) if(data[x]) break;
  6649.  
  6650.     if(x > xend) xend  = x;
  6651.   }
  6652.  
  6653.   /* If some date to print */
  6654.   if(0 <= xend) { /* Some data to write */
  6655.     ioutbuf = 0;
  6656.     xend   += 1;
  6657.  
  6658.     /* Perform vertical tab */
  6659.     if(upd->yscan != upd->yprinter) {
  6660.       step = upd->yscan - upd->yprinter;
  6661.  
  6662.       fputc(ESC,        out);
  6663.       fputc('(',        out);
  6664.       fputc('e',        out);
  6665.       fputc(2,          out);
  6666.       fputc(0,          out);
  6667.       fputc(HIGH(step), out);
  6668.       fputc(LOW(step),  out);
  6669.  
  6670.       upd->yprinter = upd->yscan;
  6671.     }
  6672.  
  6673.     for(icomp = 0; icomp < upd->ocomp; ++icomp) {
  6674.  
  6675.       /* Are there date to print for the selected color component */
  6676.       data = scan[icomp].bytes;
  6677.       for(x = 0; x <= xend; ++x) if(data[x]) break;
  6678.  
  6679.       /* Compressing of the scan line */
  6680.       if(x <= xend) {
  6681.     ioutbuf = upd_rle(upd->outbuf, scan[icomp].bytes, xend);
  6682.       } else {
  6683.     ioutbuf = 0;
  6684.       }
  6685.  
  6686.       ioutbuf1 = ioutbuf + 1;
  6687.  
  6688.       /* prints the scan line */
  6689.       fputc(ESC,            out);
  6690.       fputc('(',            out);
  6691.       fputc('A',            out);
  6692.       fputc(LOW(ioutbuf1),  out);
  6693.       fputc(HIGH(ioutbuf1), out);
  6694.       switch(upd->ocomp) {
  6695.         case 1:  fputc('K',out); break;
  6696.         case 3:
  6697.         case 4:  fputc("YMCK"[icomp],out); break;
  6698. /*
  6699.  *      Please Note: 
  6700.  *         the validity of the NCOMP-setting should be checked
  6701.  *         in the put_params-routine, thus the default-case is
  6702.  *         just a matter of coding-style.
  6703.  */
  6704.         default: fputc('K',out); break;  
  6705.       }    
  6706.  
  6707.       fwrite(upd->outbuf, 1, ioutbuf, out);
  6708.  
  6709.       fputc(CR,             out);
  6710.     }
  6711.  
  6712.     /* Printer advances one raster line */
  6713.     fputc(ESC,        out);
  6714.     fputc('(',        out);
  6715.     fputc('e',        out);
  6716.     fputc(2,          out);
  6717.     fputc(0,          out);
  6718.     fputc(HIGH(1),    out);
  6719.     fputc(LOW(1),     out);
  6720.  
  6721.     upd->yprinter += 1;
  6722.  
  6723.   }
  6724.  
  6725.   /* Advance scan by one */
  6726.   upd->yscan += 1;
  6727.  
  6728.   return 0;
  6729. }
  6730.  
  6731. /* ------------------------------------------------------------------- */
  6732. /* All the Pixel-Get Routines                                          */
  6733. /* ------------------------------------------------------------------- */
  6734.  
  6735. /* That bunch of Pixel-Get Routines */
  6736.  
  6737. private upd_proc_pxlget(upd_pxlgetnix); /* A Dummy */
  6738.  
  6739. private upd_proc_pxlget(upd_pxlget1f1); /* 1 Bit Forward */
  6740. private upd_proc_pxlget(upd_pxlget1f2);
  6741. private upd_proc_pxlget(upd_pxlget1f3);
  6742. private upd_proc_pxlget(upd_pxlget1f4);
  6743. private upd_proc_pxlget(upd_pxlget1f5);
  6744. private upd_proc_pxlget(upd_pxlget1f6);
  6745. private upd_proc_pxlget(upd_pxlget1f7);
  6746. private upd_proc_pxlget(upd_pxlget1f8);
  6747.  
  6748. private upd_proc_pxlget(upd_pxlget1r1); /* 1 Bit Reverse */
  6749. private upd_proc_pxlget(upd_pxlget1r2);
  6750. private upd_proc_pxlget(upd_pxlget1r3);
  6751. private upd_proc_pxlget(upd_pxlget1r4);
  6752. private upd_proc_pxlget(upd_pxlget1r5);
  6753. private upd_proc_pxlget(upd_pxlget1r6);
  6754. private upd_proc_pxlget(upd_pxlget1r7);
  6755. private upd_proc_pxlget(upd_pxlget1r8);
  6756.  
  6757. private upd_proc_pxlget(upd_pxlget2f1); /* 2 Bit Forward */
  6758. private upd_proc_pxlget(upd_pxlget2f2);
  6759. private upd_proc_pxlget(upd_pxlget2f3);
  6760. private upd_proc_pxlget(upd_pxlget2f4);
  6761.  
  6762. private upd_proc_pxlget(upd_pxlget2r1); /* 2 Bit Reverse */
  6763. private upd_proc_pxlget(upd_pxlget2r2);
  6764. private upd_proc_pxlget(upd_pxlget2r3);
  6765. private upd_proc_pxlget(upd_pxlget2r4);
  6766.  
  6767. private upd_proc_pxlget(upd_pxlget4f1); /* 4 Bit Forward */
  6768. private upd_proc_pxlget(upd_pxlget4f2);
  6769.  
  6770. private upd_proc_pxlget(upd_pxlget4r1); /* 4 Bit Reverse */
  6771. private upd_proc_pxlget(upd_pxlget4r2);
  6772.  
  6773. private upd_proc_pxlget(upd_pxlget8f);  /* 8 Bit Forward */
  6774. private upd_proc_pxlget(upd_pxlget8r);  /* 8 Bit Reverse */
  6775.  
  6776. private upd_proc_pxlget(upd_pxlget16f); /* 16 Bit Forward */
  6777. private upd_proc_pxlget(upd_pxlget16r); /* 16Bit Reverse */
  6778.  
  6779. private upd_proc_pxlget(upd_pxlget24f); /* 24 Bit Forward */
  6780. private upd_proc_pxlget(upd_pxlget24r); /* 24 Bit Reverse */
  6781.  
  6782. private upd_proc_pxlget(upd_pxlget32f); /* 32 Bit Forward */
  6783. private upd_proc_pxlget(upd_pxlget32r); /* 32 Bit Reverse */
  6784.  
  6785. /* Initialize Forward-Run */
  6786.  
  6787. private uint32
  6788. upd_pxlfwd(upd_p upd)
  6789. {
  6790.    if(!(upd->pxlptr = upd->gsscan)) {
  6791.  
  6792.       upd->pxlget = upd_pxlgetnix;
  6793.  
  6794.    } else {
  6795.       switch(upd->int_a[IA_COLOR_INFO].data[1]) {
  6796.          case  1: upd->pxlget = upd_pxlget1f1; break;
  6797.          case  2: upd->pxlget = upd_pxlget2f1; break;
  6798.          case  4: upd->pxlget = upd_pxlget4f1; break;
  6799.          case  8: upd->pxlget = upd_pxlget8f;  break;
  6800.          case 16: upd->pxlget = upd_pxlget16f; break;
  6801.          case 24: upd->pxlget = upd_pxlget24f; break;
  6802.          case 32: upd->pxlget = upd_pxlget32f; break;
  6803.          default:
  6804. #if UPD_MESSAGES & UPD_M_ERROR
  6805.            fprintf(stderr,"upd_pxlfwd: unsupported depth (%d)\n",
  6806.               upd->int_a[IA_COLOR_INFO].data[1]);
  6807. #endif
  6808.            upd->pxlget = upd_pxlgetnix;
  6809.            break;
  6810.       }
  6811.    }
  6812.    return (uint32) 0;
  6813. }
  6814.  
  6815. /* 1 Bit Forward */
  6816.  
  6817. private uint32
  6818. upd_pxlget1f1(upd_p upd)
  6819. {
  6820.    upd->pxlget = upd_pxlget1f2;
  6821.    return *upd->pxlptr   & 0x80 ? (uint32) 1 : (uint32) 0;
  6822. }
  6823.  
  6824. private uint32
  6825. upd_pxlget1f2(upd_p upd)
  6826. {
  6827.    upd->pxlget = upd_pxlget1f3;
  6828.    return *upd->pxlptr   & 0x40 ? (uint32) 1 : (uint32) 0;
  6829. }
  6830.  
  6831. private uint32
  6832. upd_pxlget1f3(upd_p upd)
  6833. {
  6834.    upd->pxlget = upd_pxlget1f4;
  6835.    return *upd->pxlptr   & 0x20 ? (uint32) 1 : (uint32) 0;
  6836. }
  6837.  
  6838. private uint32
  6839. upd_pxlget1f4(upd_p upd)
  6840. {
  6841.    upd->pxlget = upd_pxlget1f5;
  6842.    return *upd->pxlptr   & 0x10 ? (uint32) 1 : (uint32) 0;
  6843. }
  6844.  
  6845. private uint32
  6846. upd_pxlget1f5(upd_p upd)
  6847. {
  6848.    upd->pxlget = upd_pxlget1f6;
  6849.    return *upd->pxlptr   & 0x08 ? (uint32) 1 : (uint32) 0;
  6850. }
  6851.  
  6852. private uint32
  6853. upd_pxlget1f6(upd_p upd)
  6854. {
  6855.    upd->pxlget = upd_pxlget1f7;
  6856.    return *upd->pxlptr   & 0x04 ? (uint32) 1 : (uint32) 0;
  6857. }
  6858.  
  6859. private uint32
  6860. upd_pxlget1f7(upd_p upd)
  6861. {
  6862.    upd->pxlget = upd_pxlget1f8;
  6863.    return *upd->pxlptr   & 0x02 ? (uint32) 1 : (uint32) 0;
  6864. }
  6865.  
  6866. private uint32
  6867. upd_pxlget1f8(upd_p upd)
  6868. {
  6869.    upd->pxlget = upd_pxlget1f1;
  6870.    return *upd->pxlptr++ & 0x01 ? (uint32) 1 : (uint32) 0;
  6871. }
  6872.  
  6873. /* 2 Bit Forward */
  6874.  
  6875. private uint32
  6876. upd_pxlget2f1(upd_p upd)
  6877. {
  6878.    upd->pxlget = upd_pxlget2f2;
  6879.    return ((uint32) (*upd->pxlptr  ) & (uint32) 0xC0) >> 6;
  6880. }
  6881.  
  6882. private uint32
  6883. upd_pxlget2f2(upd_p upd)
  6884. {
  6885.    upd->pxlget = upd_pxlget2f3;
  6886.    return ((uint32) (*upd->pxlptr  ) & (uint32) 0x30) >> 4;
  6887. }
  6888.  
  6889. private uint32
  6890. upd_pxlget2f3(upd_p upd)
  6891. {
  6892.    upd->pxlget = upd_pxlget2f4;
  6893.    return ((uint32) (*upd->pxlptr  ) & (uint32) 0x0C) >> 2;
  6894. }
  6895.  
  6896. private uint32
  6897. upd_pxlget2f4(upd_p upd)
  6898. {
  6899.    upd->pxlget = upd_pxlget2f1;
  6900.    return  (uint32) (*upd->pxlptr++) & (uint32) 0x03;
  6901. }
  6902.  
  6903.  
  6904. /* 4 Bit Forward */
  6905. private uint32
  6906. upd_pxlget4f1(upd_p upd)
  6907. {
  6908.    upd->pxlget = upd_pxlget4f2;
  6909.    return ((uint32) (*upd->pxlptr  ) & (uint32) 0xF0) >> 4;
  6910. }
  6911.  
  6912. private uint32
  6913. upd_pxlget4f2(upd_p upd)
  6914. {
  6915.    upd->pxlget = upd_pxlget4f1;
  6916.    return  (uint32) (*upd->pxlptr++) & (uint32) 0x0F;
  6917. }
  6918.  
  6919.  
  6920. /* 8 Bit Forward */
  6921. private uint32
  6922. upd_pxlget8f(upd_p upd)
  6923. {
  6924.    return (uint32) (*upd->pxlptr++);
  6925. }
  6926.  
  6927.  
  6928. /* 16 Bit Forward */
  6929. private uint32
  6930. upd_pxlget16f(upd_p upd)
  6931. {
  6932.    uint32 ci  = (uint32) (*upd->pxlptr++) << 8;
  6933.                   ci |=                   *upd->pxlptr++;
  6934.    return         ci;
  6935. }
  6936.  
  6937. /* 24 Bit Forward */
  6938. private uint32
  6939. upd_pxlget24f(upd_p upd)
  6940. {
  6941.    uint32 ci  = (uint32) (*upd->pxlptr++) << 16;
  6942.           ci |= (uint32) (*upd->pxlptr++) <<  8;
  6943.           ci |=           *upd->pxlptr++;
  6944.    return ci;
  6945. }
  6946.  
  6947. /* 32 Bit Forward */
  6948. private uint32
  6949. upd_pxlget32f(upd_p upd)
  6950. {
  6951.    uint32 ci  = (uint32) (*upd->pxlptr++) << 24;
  6952.                   ci |= (uint32) (*upd->pxlptr++) << 16;
  6953.                   ci |= (uint32) (*upd->pxlptr++) <<  8;
  6954.                   ci |=                   *upd->pxlptr++;
  6955.    return         ci;
  6956. }
  6957.  
  6958.  
  6959. /* Dummy-Routine */
  6960.  
  6961. private uint32
  6962. upd_pxlgetnix(upd_p upd)
  6963. {
  6964.    return (uint32) 0;
  6965. }
  6966.  
  6967. /* Initialize Reverse-Run */
  6968.  
  6969. private uint32
  6970. upd_pxlrev(upd_p upd)
  6971. {
  6972.    const uint width = upd->pwidth < upd->gswidth ? upd->pwidth : upd->gswidth;
  6973.  
  6974.    if(!(upd->pxlptr = upd->gsscan)) {
  6975.  
  6976.       upd->pxlget = upd_pxlgetnix;
  6977.  
  6978.    } else {
  6979.       uint32 ofs = (uint32) upd->int_a[IA_COLOR_INFO].data[1] * (width-1);
  6980.  
  6981.       upd->pxlptr += ofs>>3;
  6982.  
  6983.       ofs &= 7;
  6984.  
  6985.       switch(upd->int_a[IA_COLOR_INFO].data[1]) {
  6986.          case  1: switch(ofs) {
  6987.                case 0:  upd->pxlget = upd_pxlget1r1; break;
  6988.                case 1:  upd->pxlget = upd_pxlget1r2; break;
  6989.                case 2:  upd->pxlget = upd_pxlget1r3; break;
  6990.                case 3:  upd->pxlget = upd_pxlget1r4; break;
  6991.                case 4:  upd->pxlget = upd_pxlget1r5; break;
  6992.                case 5:  upd->pxlget = upd_pxlget1r6; break;
  6993.                case 6:  upd->pxlget = upd_pxlget1r7; break;
  6994.                case 7:  upd->pxlget = upd_pxlget1r8; break;
  6995.             } break;
  6996.          case  2: switch(ofs) {
  6997.                case 0:  upd->pxlget = upd_pxlget2r1; break;
  6998.                case 2:  upd->pxlget = upd_pxlget2r2; break;
  6999.                case 4:  upd->pxlget = upd_pxlget2r3; break;
  7000.                case 6:  upd->pxlget = upd_pxlget2r4; break;
  7001.             } break;
  7002.          case  4: switch(ofs) {
  7003.                case 0:  upd->pxlget = upd_pxlget4r1; break;
  7004.                case 4:  upd->pxlget = upd_pxlget4r2; break;
  7005.             } break;
  7006.          case  8: upd->pxlget = upd_pxlget8r;  break;
  7007.          case 16:
  7008.             upd->pxlget  = upd_pxlget16r;
  7009.             upd->pxlptr += 1;
  7010.             break;
  7011.          case 24:
  7012.             upd->pxlget = upd_pxlget24r;
  7013.             upd->pxlptr += 2;
  7014.             break;
  7015.          case 32:
  7016.             upd->pxlget = upd_pxlget32r;
  7017.             upd->pxlptr += 3;
  7018.             break;
  7019.          default:
  7020. #if UPD_MESSAGES & UPD_M_ERROR
  7021.            fprintf(stderr,"upd_pxlrev: unsupported depth (%d)\n",
  7022.               upd->int_a[IA_COLOR_INFO].data[1]);
  7023. #endif
  7024.            upd->pxlget = upd_pxlgetnix;
  7025.            break;
  7026.       }
  7027.    }
  7028.    return (uint32) 0;
  7029. }
  7030.  
  7031. /* 1 Bit Reverse */
  7032.  
  7033. private uint32
  7034. upd_pxlget1r1(upd_p upd)
  7035. {
  7036.    upd->pxlget = upd_pxlget1r8;
  7037.    return *upd->pxlptr-- & 0x80 ? (uint32) 1 : (uint32) 0;
  7038. }
  7039.  
  7040. private uint32
  7041. upd_pxlget1r2(upd_p upd)
  7042. {
  7043.    upd->pxlget = upd_pxlget1r1;
  7044.    return *upd->pxlptr   & 0x40 ? (uint32) 1 : (uint32) 0;
  7045. }
  7046.  
  7047. private uint32
  7048. upd_pxlget1r3(upd_p upd)
  7049. {
  7050.    upd->pxlget = upd_pxlget1r2;
  7051.    return *upd->pxlptr   & 0x20 ? (uint32) 1 : (uint32) 0;
  7052. }
  7053.  
  7054. private uint32
  7055. upd_pxlget1r4(upd_p upd)
  7056. {
  7057.    upd->pxlget = upd_pxlget1r3;
  7058.    return *upd->pxlptr   & 0x10 ? (uint32) 1 : (uint32) 0;
  7059. }
  7060.  
  7061. private uint32
  7062. upd_pxlget1r5(upd_p upd)
  7063. {
  7064.    upd->pxlget = upd_pxlget1r4;
  7065.    return *upd->pxlptr   & 0x08 ? (uint32) 1 : (uint32) 0;
  7066. }
  7067.  
  7068. private uint32
  7069. upd_pxlget1r6(upd_p upd)
  7070. {
  7071.    upd->pxlget = upd_pxlget1r5;
  7072.    return *upd->pxlptr   & 0x04 ? (uint32) 1 : (uint32) 0;
  7073. }
  7074.  
  7075. private uint32
  7076. upd_pxlget1r7(upd_p upd)
  7077. {
  7078.    upd->pxlget = upd_pxlget1r6;
  7079.    return *upd->pxlptr   & 0x02 ? (uint32) 1 : (uint32) 0;
  7080. }
  7081.  
  7082. private uint32
  7083. upd_pxlget1r8(upd_p upd)
  7084. {
  7085.    upd->pxlget = upd_pxlget1r7;
  7086.    return *upd->pxlptr   & 0x01 ? (uint32) 1 : (uint32) 0;
  7087. }
  7088.  
  7089. /* 2 Bit Reverse */
  7090.  
  7091. private uint32
  7092. upd_pxlget2r1(upd_p upd)
  7093. {
  7094.    upd->pxlget = upd_pxlget2r4;
  7095.    return ((uint32) (*upd->pxlptr--) & (uint32) 0xC0) >> 6;
  7096. }
  7097.  
  7098. private uint32
  7099. upd_pxlget2r2(upd_p upd)
  7100. {
  7101.    upd->pxlget = upd_pxlget2r1;
  7102.    return ((uint32) (*upd->pxlptr  ) & (uint32) 0x30) >> 4;
  7103. }
  7104.  
  7105. private uint32
  7106. upd_pxlget2r3(upd_p upd)
  7107. {
  7108.    upd->pxlget = upd_pxlget2r2;
  7109.    return ((uint32) (*upd->pxlptr  ) & (uint32) 0x0C) >> 2;
  7110. }
  7111.  
  7112. private uint32
  7113. upd_pxlget2r4(upd_p upd)
  7114. {
  7115.    upd->pxlget = upd_pxlget2r3;
  7116.    return  (uint32) (*upd->pxlptr  ) & (uint32) 0x03;
  7117. }
  7118.  
  7119. /* 4 Bit Reverse */
  7120.  
  7121. private uint32
  7122. upd_pxlget4r1(upd_p upd)
  7123. {
  7124.    upd->pxlget = upd_pxlget4r2;
  7125.    return ((uint32) (*upd->pxlptr--) & (uint32) 0xF0) >> 4;
  7126. }
  7127.  
  7128. private uint32
  7129. upd_pxlget4r2(upd_p upd)
  7130. {
  7131.    upd->pxlget = upd_pxlget4r1;
  7132.    return  (uint32) (*upd->pxlptr  ) & (uint32) 0x0F;
  7133. }
  7134.  
  7135.  
  7136. /* 8 Bit Reverse */
  7137. private uint32
  7138. upd_pxlget8r(upd_p upd)
  7139. {
  7140.    return (uint32) (*upd->pxlptr--);
  7141. }
  7142.  
  7143.  
  7144. /* 16 Bit Reverse */
  7145. private uint32
  7146. upd_pxlget16r(upd_p upd)
  7147. {
  7148.    uint32 ci  =                   *upd->pxlptr--;
  7149.                   ci |= (uint32) (*upd->pxlptr--) << 8;
  7150.    return         ci;
  7151. }
  7152.  
  7153. /* 24 Bit Reverse */
  7154. private uint32
  7155. upd_pxlget24r(upd_p upd)
  7156. {
  7157.    uint32 ci  =           *upd->pxlptr--;
  7158.           ci |= (uint32) (*upd->pxlptr--) <<  8;
  7159.           ci |= (uint32) (*upd->pxlptr--) << 16;
  7160.    return ci;
  7161. }
  7162.  
  7163. /* 32 Bit Reverse */
  7164. private uint32
  7165. upd_pxlget32r(upd_p upd)
  7166. {
  7167.    uint32 ci  =                   *upd->pxlptr--;
  7168.                   ci |= (uint32) (*upd->pxlptr--) <<  8;
  7169.                   ci |= (uint32) (*upd->pxlptr--) << 16;
  7170.                   ci |= (uint32) (*upd->pxlptr--) << 24;
  7171.    return         ci;
  7172. }
  7173.